在android中调整谷歌地图(api v2)缩放级别

San*_*osh 2 android google-maps android-fragments

maps api v2我正在使用我的应用程序.我必须在地图上显示我当前的位置和目标位置,以便两个位置在屏幕上可见(在最大可能的缩放级别).这是我到目前为止所尝试的......

googleMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.mapFragment)).getMap();

 if(googleMap != null){

        googleMap.setMyLocationEnabled(true);
        LatLng targetLocationLatLng = new LatLng(modelObject.getLattitude(), modelObject.getLongitude());
        LatLng currentLocationLatLng = new LatLng(this.currentLocationLattitude, this.currentLocationLongitude);
        googleMap.addMarker(new MarkerOptions().position(targetLocationLatLng).title(modelObject.getLocationName()).icon(BitmapDescriptorFactory.fromResource(R.drawable.location_icon)));
        LatLngBounds bounds = new LatLngBounds(currentLocationLatLng, targetLocationLatLng);
        googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 3));

    }
Run Code Online (Sandbox Code Playgroud)

应用程序因以下原因而关闭: java.lang.IllegalStateException: Map大小不应为0.很可能,layout地图视图尚未发生.

如何获得最大可能的缩放级别?请帮我.

Mat*_*ndy 19

在我的项目中,我使用了 com.google.android.gms.maps.model.LatLngBounds.Builder

适应您的源代码,它应该看起来像这样:

Builder boundsBuilder = new LatLngBounds.Builder();
boundsBuilder.include(currentLocationLatLng);
boundsBuilder.include(targetLocationLatLng);
// pan to see all markers on map:
LatLngBounds bounds = boundsBuilder.build();
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 3));
Run Code Online (Sandbox Code Playgroud)