-1

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));
        }
    }
Community
  • 1
  • 1
Rashad.Z
  • 2,494
  • 2
  • 27
  • 58
  • Possible duplicate of [Android map v2 zoom to show all the markers](http://stackoverflow.com/questions/14828217/android-map-v2-zoom-to-show-all-the-markers) – AL. May 26 '16 at 08:55
  • if you look at the answer in the link you posted first, you will see that this was exactly what i did – Rashad.Z May 26 '16 at 08:57
  • The answer's flow is he iterated to all the current available markers. I suggest creating/modifying your current zoom function, to have List of `LatLng` parameter, then for each `LatLng` value call `inlcude()`, process the builder, then try `animateCamera()` then. – AL. May 26 '16 at 09:02

1 Answers1

0

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));
Sreehari
  • 5,621
  • 2
  • 25
  • 59
  • this will only zoom to one of the markers... and not both markers as i said i want – Rashad.Z May 26 '16 at 08:51
  • Zooming more than one location is not possible. If you have two markers, calculate the center point and then calculating the radius, you can manipulate the zoom level according to the derived radius. If you want I can put up the code which will zoom according to given radius alone. – Sreehari May 26 '16 at 09:12
  • yeah ok i will do that. and yes if can – Rashad.Z May 26 '16 at 09:23