谷歌地图Android LatLngBounds

时间:2016-12-15 15:47:04

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

我在添加标记后使用以下代码缩放到地图上的最大缩放级别。它可以工作,但只有我在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);

3 个答案:

答案 0 :(得分:2)

    @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));
            }
        });
    }

答案 1 :(得分:1)

您可能需要等待地图加载的回调才可以缩放它:

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':app'.
> Could not resolve all dependencies for configuration ':app:_debugCompile'.
> Could not find com.honeywell.aidc:AidcManager:.
 Searched in the following locations:
         file:/Users/XXX/.m2/repository/com/honeywell/aidc/AidcManager//AidcManager-.pom
     file:/Users/XXX/.m2/repository/com/honeywell/aidc/AidcManager//AidcManager-.jar
     https://jcenter.bintray.com/com/honeywell/aidc/AidcManager//AidcManager-.pom
     https://jcenter.bintray.com/com/honeywell/aidc/AidcManager//AidcManager-.jar
     file:/XXX/node_modules/react-native/android/com/honeywell/aidc/AidcManager//AidcManager-.pom
     file:/XXX/node_modules/react-native/android/com/honeywell/aidc/AidcManager//AidcManager-.jar
     file:/Users/XXX/Library/Android/sdk/extras/android/m2repository/com/honeywell/aidc/AidcManager//AidcManager-.pom
     file:/Users/XXX/Library/Android/sdk/extras/android/m2repository/com/honeywell/aidc/AidcManager//AidcManager-.jar
     file:/Users/XXX/Library/Android/sdk/extras/google/m2repository/com/honeywell/aidc/AidcManager//AidcManager-.pom
     file:/Users/XXX/Library/Android/sdk/extras/google/m2repository/com/honeywell/aidc/AidcManager//AidcManager-.jar

Required by:
     MyApp:app:unspecified

答案 2 :(得分:0)

您需要立即显示地图:

mMapView.onResume();