缩放到适合在地图上的谷歌地图V2所有标记(zoom to fit all markers on ma

2019-09-02 03:17发布

我想设置地图缩放级别,以适应在地图上的所有标记。 我用下面的方法被很多人说,但它不是为我工作。 这是显示一些其他的点。

if (positions.size() > 0) {

    final LatLngBounds.Builder builder = new LatLngBounds.Builder();
    for (Marker m : positions) {
        builder.include(m.getPosition());
    }
    builder.include(positions.get(i).getPosition());
}

try {
    googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {

        @Override
        public void onCameraChange(CameraPosition arg0) {

            googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(
                    builder.build(), UserDataManager.getInstance().getWidth(), 
                    UserDataManager.getInstance().getWidth(),0));
            googleMap.setOnCameraChangeListener(null);

        }
    });

} catch (IllegalStateException e) {
    // TODO: handle exception
    final View mapView = getSupportFragmentManager()
            .findFragmentById(R.id.map).getView();
    if (mapView.getViewTreeObserver().isAlive()) {
        mapView.getViewTreeObserver().addOnGlobalLayoutListener(
            new OnGlobalLayoutListener() {

                // We check which build version we are using.
                @Override
                public void onGlobalLayout() {
                    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                        mapView.getViewTreeObserver()
                                .removeGlobalOnLayoutListener(
                                        this);
                    } else {
                        mapView.getViewTreeObserver()
                                .removeOnGlobalLayoutListener(
                                        this);
                    }

                    googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {

                        @Override
                        public void onCameraChange(CameraPosition arg0) {

                            googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(
                                    builder.build(), 
                                    UserDataManager.getInstance().getWidth(), 
                                    UserDataManager.getInstance().getWidth(),0));
                            googleMap.setOnCameraChangeListener(null);

                        }
                    });

                }
            });
        }
    }
}

Answer 1:

尝试使用此

//Calculate the markers to get their position
LatLngBounds.Builder b = new LatLngBounds.Builder();
for (Marker m : markers) {
    b.include(m.getPosition());
}
LatLngBounds bounds = b.build();
//Change the padding as per needed
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 25,25,5);
mMap.animateCamera(cu);

选中此谷歌地图V2库 ,我已经创建了一个包含此功能只需使用

Marker m1,m2, m3, m4 .... ;

mapHelper.zoomToFitMarkers(m1,m2, m3, m4 ....);

编辑:

获取当前位置

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//You can use GPS_PROVIDER also
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            currentLatitude = location.getLatitude();
            currentLongitude = location.getLongitude();
            //Add your marker here and zoom to fit it
            //mapHelper.zoomToFitMarkers(m1,m2, m3, m4,mapHelper.addMarker(currentLatitude,currentLongitude,"Current Location"););
        }
    });


Answer 2:

mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 15));


文章来源: zoom to fit all markers on map google maps v2