I'm building an app where I load data from the API in onCameraIdle
. The request needs the target location and a radius in which to give me the results.
The code I use to get the radius from the current camera is:
/**
* @param map The Map on which to compute the radius.
* @return The circle radius of the area visible on map, in meters.
*/
public float getRadiusVisibleOnMap(GoogleMap map) {
VisibleRegion visibleRegion = map.getProjection().getVisibleRegion();
LatLng farRight = visibleRegion.farRight;
LatLng farLeft = visibleRegion.farLeft;
LatLng nearRight = visibleRegion.nearRight;
LatLng nearLeft = visibleRegion.nearLeft;
float[] distanceWidth = new float[1];
Location.distanceBetween(
(farRight.latitude + nearRight.latitude) / 2,
(farRight.longitude + nearRight.longitude) / 2,
(farLeft.latitude + nearLeft.latitude) / 2,
(farLeft.longitude + nearLeft.longitude) / 2,
distanceWidth
);
float[] distanceHeight = new float[1];
Location.distanceBetween(
(farRight.latitude + nearRight.latitude) / 2,
(farRight.longitude + nearRight.longitude) / 2,
(farLeft.latitude + nearLeft.latitude) / 2,
(farLeft.longitude + nearLeft.longitude) / 2,
distanceHeight
);
float radius;
if (distanceWidth[0] > distanceHeight[0]) {
radius = distanceWidth[0];
} else {
radius = distanceHeight[0];
}
return radius;
}
I got it from here
There are some cases where I need to get the radius knowing only the zoom level, so that the info is already loaded on the map when the zoom animation is finished.
I tried to adjust the code from here to get the radius knowing the zoom level instead of the other way around.
public int getZoomLevel(Circle circle) {
if (circle != null) {
double radius = circle.getRadius();
double scale = radius / 500;
zoomLevel = (int) (16 - Math.log(scale) / Math.log(2));
}
return zoomLevel;
}
I got the formula:
The formula is incorrect. It doesn't output the right dimensions. For a zoom level of 14, I get a radius dimension of 2.697331km using the camera's VisibleRegion. If I calculate the radius using the formula, the result is 0.008km.