android - Google map Android LatLngBounds

标签 android google-maps google-maps-android-api-2

添加标记后,我使用以下代码在 map 上缩放至最大缩放级别。它可以工作,但前提是我让它在 500 毫秒的延迟运行时间内。如果取出来的话,就没有作用了。我知道我错过了一些明显的东西,但我不知道我错过了什么。谁能告诉我需要将 LatLngBounds 代码放在哪里才能使其在没有 Runnable 的情况下工作?

// Expand our Zoom to fit all markers
        LatLngBounds.Builder builder = new LatLngBounds.Builder();

        //the include method will calculate the min and max bound.
        builder.include(myLocationPin.getPosition());
        builder.include(placeLocationPin.getPosition());

        final LatLngBounds bounds = builder.build();

        final int zoomWidth = getResources().getDisplayMetrics().widthPixels;
        final int zoomHeight = getResources().getDisplayMetrics().heightPixels;
        final int zoomPadding = (int) (zoomWidth * 0.10); // offset from edges of the map 12% of screen

        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds,zoomWidth,zoomHeight,zoomPadding));
            }
        }, 500);

最佳答案

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_map);

        map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();

        map.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
            @Override
            public void onMapLoaded() {
                map.addMarker(new MarkerOptions().title("your title")
                        .snippet("your desc")
                        .position(new LatLng(-22.2323,-2.32323)));
                map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds,zoomWidth,
                                                                zoomHeight,zoomPadding));
            }
        });
    }

关于android - Google map Android LatLngBounds,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41168105/

相关文章:

c# - 通过单击将标记添加到 map

java - GMap 信息窗口不显示

android - 如何在 Fragment 中获取 Google Maps 对象

android - 使用 APK 拆分进行发布但不使用调试构建类型

java - 回收商聊天分页在顶部添加新消息

Android动态改变APP主题

android - 即使更新了发布 key 的 SHA 1 指纹后,谷歌地图在 Play 商店发布后仍未加载

java - 在 onNavigationItemSelected 上获取微调器的项目文本

android - 第一次在 google maps android 中不调用 onMapLoaded(),onMapLoaded 在 google maps android 中不起作用

android - 如何在 fragment 中使用谷歌地图 V2?