4

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.

Pravinsingh Waghela
  • 2,516
  • 4
  • 32
  • 50
  • What do you mean by "zoom to desire(sic) miles radius"? Zoom is a dimensionless unit. Do you mean you want the circle to fill the screen, or say 50% of the height of the screen? As it stands this question is meaningless. – NickT Sep 08 '16 at 08:38
  • @NickT I need when I zoom the screen should show the circle only in the screen. – Pravinsingh Waghela Sep 08 '16 at 09:21
  • Again I ask - how big do you want the circle? Fill the screen height? Fill the screen width? Take up half the screen? – NickT Sep 08 '16 at 09:36
  • @NickT I am drawing the circle with radius of some specified miles say 10 miles. Now I want when the map zoom it should only show the circle edges or perimeter in the screen. If I increase the miles to say 40 miles then the circle is again drawn with 40 miles radius, I again need the map should be zoom to the extend where again my circle edges are just shown in the screen. Not more than that and not less than that. – Pravinsingh Waghela Sep 08 '16 at 09:59
  • @NickT I had edited the question with more details. Please have the look – Pravinsingh Waghela Sep 08 '16 at 10:27

2 Answers2

7

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;
    }
Pravinsingh Waghela
  • 2,516
  • 4
  • 32
  • 50
  • 2
    Why do you simply divide radius by 500? Zoom level depends on screen size, dpi, so it will show different results on different devices. – CoolMind Jun 04 '19 at 16:16
  • I tried this code. It gives wrong results. A circle becomes larger than a screen. This code has been gotten from https://stackoverflow.com/a/13159839/2914140. – CoolMind Jun 05 '19 at 08:08
  • Doesn't work on different screen sizes. For example, for a 10-inch, 7-inch tablet doesn't work. – wonderingdev Jun 08 '23 at 20:26
  • @wonderingdev the scale value where we are diving the radius by 500. You can work there to fit it for different screen sizes. Just try to make it dynamic rather than 500. – Pravinsingh Waghela Jun 20 '23 at 09:47
2

You need to find the distance across the screen when the view gets refreshed. I'm not too familiar with the new APIs and views but something like this should work:

  LatLngBounds bounds = map.getProjection().getVisibleRegion().latLngBounds;
        double llNeLat = bounds.northeast.latitude;
        double llSwLat = bounds.southwest.latitude;
        double llNeLng = bounds.northeast.longitude;
        double llSwLng = bounds.southwest.longitude;
        float results[] = new float[5];
        Location.distanceBetween(llNeLat, llNeLng, llSwLat, llSwLng, results);
        float radius = results[0];
        // radius is distance top right to bottom left on screen in metres
        // so use maybe 3/4 of this, then conert your miles to that value
        // in metres

then add that value to the addCircle invocation

NickT
  • 23,844
  • 11
  • 78
  • 121