Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Google Map zoom level into km

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.

like image 609
Zohaib Akram Avatar asked Jan 05 '17 09:01

Zohaib Akram


3 Answers

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
like image 197
lambda Avatar answered Sep 23 '22 02:09

lambda


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

enter image description here

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

like image 42
antonio Avatar answered Sep 22 '22 02:09

antonio


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)

like image 27
Eta Avatar answered Sep 24 '22 02:09

Eta