Android: How to Set the Zoom Level of Map View to 1 Km Radius Around My Current Location

Android: How do I set the zoom level of map view to 1 km radius around my current location?

The following code is what ended up using. Given the screen width and the fact that at zoom level 1 the equator of Earth is 256 pixels long and every subsequent zoom level doubles the number of pixels needed to represent earths equator, the following function returns the zoom level where the screen will show an area of 2Km width.

private int calculateZoomLevel(int screenWidth) {
double equatorLength = 40075004; // in meters
double widthInPixels = screenWidth;
double metersPerPixel = equatorLength / 256;
int zoomLevel = 1;
while ((metersPerPixel * widthInPixels) > 2000) {
metersPerPixel /= 2;
++zoomLevel;
}
Log.i("ADNAN", "zoom level = "+zoomLevel);
return zoomLevel;
}

Zooming Google map to specific radius in miles in android

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

Android - Compute distance of map every zoom level like Gmaps scale bar

You can find the radius between right/left most point and center of the map when map becomes idle by using

/**
* Method used to fetch Map radius
*/
private int getMapRadius() {
LatLng latlng = googleMap.getProjection().getVisibleRegion().latLngBounds.getCenter();
if (latLng == null)
return 0;
LatLng latLng1 = googleMap.getProjection().getVisibleRegion().farRight;
return (int) MapUtils.computeDistance(latLng1.latitude, latLng1.longitude, latLng.latitude, latLng.longitude);
}

Convert Google Map zoom level into km

You can calculate the distance between the center of the map and the top left coordinate like this:

VisibleRegion visibleRegion = mMap.getProjection().getVisibleRegion();
double distance = SphericalUtil.computeDistanceBetween(
visibleRegion.farLeft, mMap.getCameraPosition().target);

Sample Image

Note that I'm using the SphericalUtil.computeDistanceBetween method from the Google Maps Android API Utility Library.



Related Topics



Leave a reply



Submit