Can anyone help me to calculate or convert google map camera zoom level into KM distance . I want to send distance to api to get more pin data when user zoom out google map.
I have my simple conversion of zoom level
(zl) to km.
If zl1 = 40,000km
and zl2 = 20,000km
and so on. So every level will half the km.Therefore,
km = ( 40000/2 ^ zl ) * 2
e.g. zl = 4
km = (40000/2^4) * 2
= 5000km
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);
Note that I'm using the SphericalUtil.computeDistanceBetween
method from the Google Maps Android API Utility Library.
I really like the simplicity of @lambda solution but it seems zoom levels have been rescaled.
When I do my tests I have zl3 = 38Mm , zl6 = 4.8Mm , zl9 = 600km ... BUT this is only reliable on the equator, you have to add another multiplier to account for latitude.
So my final formule looks like this :
km = (38000 / 2 ^ ( zl - 3)) * cos(lat)
with zl = map zoom level and lat = latitude of map's center in degre
For JS user you can copy this :
const km = 38000 / Math.pow(2, zl - 3) * Math.cos(lat * Math.PI / 180);
And now it is time to make the reverse formula :
zl = log2(38000 * cos(lat) / km) + 3
Which translate to JS as :
const zl = Math.log2(38000 * Math.cos ( lat * Math.PI / 180) / km) + 3
PS : I don't know if resolution have an impact, I am using a full HD screen (1920 x 1080)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With