简体   繁体   中英

google maps camera position not changing

THIS IS NOT A DUPLICATE OF Android map v2 zoom to show all the markers

i tried the answer posted in the above link and it did not work for me and that is why i asked the question

i am displaying two markers on google maps. the first time the map is loaded and the map is set, the zoom is set correctly. but when the marker position changes, the zoom level is not changing.

here is my code. i want the zoom level to change when the marker location is changed and i want to scale the map in a way to include BOTH markers

        SubscriberMarker = map.addMarker(new MarkerOptions().position(new LatLng(SUBSCRIBER_LAT, SUBSCRIBER_LNG)).title("Home Location").icon(BitmapDescriptorFactory.fromResource(R.drawable.home)));
        builder.include(SubscriberMarker.getPosition());

        _MyBound = builder.build();



// this part is called when the location is changed: 

                if (FTMarker!=null)
                {
                    FTMarker.remove();
                }

                FTMarker = googleMap.addMarker(new MarkerOptions().position(new LatLng(LAT,LNG)).title("FT Location").icon(BitmapDescriptorFactory.fromResource(R.drawable.woker)));

                builder.include(FTMarker.getPosition());

                _MyBound = builder.build();




// the zoom function is called after that
    public void zoom()
    {

        if (mapView!=null) {
            int padding_percent = 20;
            int padding = Math.min(mapView.getHeight(), mapView.getWidth()) * padding_percent / 100;
            googleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(_MyBound, mapView.getWidth(), mapView.getHeight(), padding));
        }
    }

After adding marker if you want to animate to the same, do the following

LatLng toPosition=new LatLng(dummy_lat,dummy_lon);

MarkerOptions markerOptions = new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.woker));//set marker custom icon if needed
markerOptions.position(toPosition);//set marker position
markerOptions.title("Marker Title");//set title for marker
mGoogleMap.addMarker(markerOptions);//add marker to map

mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(toPosition, 18.0f));//animate to the marker location

Zoom level according to radius can be found by

double circleRad = dummy_radius*1000;//multiply by 1000 to make units in KM

Dummy_radius should be calculated according to your first LatLon and second LatLon

private int getZoomLevel(double radius){
            double scale = radius / 500;
            return ((int) (16 - Math.log(scale) / Math.log(2)));
}

float zoomLevel = getZoomLevel(circleRad);
mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(LatLon, zoomLevel));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM