缩放以适应地图上的所有标记谷歌地图v2

时间:2013-05-07 09:49:18

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

我想设置地图缩放级别以适合地图上的所有标记。我使用了许多人所说的方法,但它不适合我。它显示了其他一些观点。

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

                        }
                    });

                }
            });
        }
    }
}

3 个答案:

答案 0 :(得分:50)

尝试使用此

//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);

检查我创建的包含此功能的Google Maps v2 library 只需使用

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

答案 1 :(得分:4)

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

答案 2 :(得分:0)

经过多次搜索,我找到了最合适的答案:

//LatLngBound will cover all your marker on Google Maps
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (int i = 0; i < latLng.size(); i++) {
                        LatLng customMarkerLocation = new LatLng(latLng.get(i).getLat(), latLng.get(i).getLng());
                        mMap.addMarker(new MarkerOptions().position(customMarkerLocation).
                                icon(BitmapDescriptorFactory.fromBitmap(
                                        createCustomMarker(getActivity(), R.drawable.ic_pepsi, "Manish")))).setTitle("iPragmatech Solutions Pvt Lmt");

                        builder.include(customMarkerLocation); //include Point to be covered

                    }

                    LatLngBounds bounds = builder.build();
                    int width = getResources().getDisplayMetrics().widthPixels;
                    int height = getResources().getDisplayMetrics().heightPixels;
                    int padding = (int) (width * 0.15); // offset from edges of the map 15% of screen

                    // to animate camera with some padding and bound -cover- all markers
                    CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, width, height, padding);
                    mMap.animateCamera(cu);

参考: Adding custom image in Google Maps Marker, zoom in to keep all markers in the view