Zooming Google map to specific radius in miles in android

Pravinsingh Waghela picture Pravinsingh Waghela · Sep 8, 2016 · Viewed 7.6k times · Source

I want to zoom the Google map to a specific radius in miles say 10 miles/20 miles/30 miles etc as per my condition in android.

What I need is to draw a circle of specific radius (10/20/30.. miles) from the current lat long point and zoom the map to that particular miles for which i have draw the circle with radius.

I am able to point my current position, Draw the circle. But I am not able to zoom the map to desired radius mile only(circle should be focus on the screen with specified miles with my current position as center).

Currently I am using the following code to zoom:

gooMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(selectedLat, selectedLong), 15));
                    gooMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
                    Log.e("Circle Lat Long:", selectedLat + ", " + selectedLong);
                    circle.remove();
                    circle = gooMap.addCircle(new CircleOptions()
                            .center(new LatLng(selectedLat, selectedLong))
                            .radius(iMiles * 1609.34) // Converting Miles into Meters...
                            .strokeColor(Color.RED)
                            .strokeWidth(5));
                    circle.isVisible();

I knew that giving the zoom level to 15 will not zoom the map to the desire miles. But I need to zoom the map to desire miles radius. how can i achieve it. Can anyone can help me for the same. Edit:- Image added explains what I need in details. Image_1:- When I set my radius to say 10 miles then the map should zoom as shown. Image_2 When I set my radius to say 50 miles then the map should zoom as shown. Please see the difference in the map places to analysis the zoom level I need. Image_1 Image_2 Thanks in advance.

Answer

Pravinsingh Waghela picture Pravinsingh Waghela · Sep 27, 2016

I got the solution for the same, Here is the one:

// Zoom in, animating the camera.
                double iMeter = iMiles * 1609.34;
                circle.remove();
                circle = gooMap.addCircle(new CircleOptions()
                        .center(new LatLng(selectedLat, selectedLong))
                        .radius(iMeter) // Converting Miles into Meters...
                        .strokeColor(Color.RED)
                        .strokeWidth(5));
                circle.isVisible();
                float currentZoomLevel = getZoomLevel(circle);
                float animateZomm = currentZoomLevel + 5;

                Log.e("Zoom Level:", currentZoomLevel + "");
                Log.e("Zoom Level Animate:", animateZomm + "");

                gooMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(selectedLat, selectedLong), animateZomm));
                gooMap.animateCamera(CameraUpdateFactory.zoomTo(currentZoomLevel), 2000, null);
                Log.e("Circle Lat Long:", selectedLat + ", " + selectedLong);

And our method that calculate the zoom level as per device is as follows:

public float getZoomLevel(Circle circle) {
        float zoomLevel=0;
        if (circle != null){
            double radius = circle.getRadius();
            double scale = radius / 500;
            zoomLevel =(int) (16 - Math.log(scale) / Math.log(2));
        }
        return zoomLevel +.5f;
    }