Search code examples
google-mapslatitude-longitudegoogle-maps-static-api

Get dimensions of Google Static Maps in degrees/kilometers


I was wondering if there is any way of getting the dimensions (in degrees or kilometers) of a google static map image, given the zoom level and the size of the map in pixels.

I have seen a formula to get the longitude in degrees:

(widthInPixels/256)*(360/pow(2,zoomLevel));

And this is pretty accurate. However the ratio between km and degrees changes depending on how close you are to the poles or the equator, so this formula won't work (even if I substitute the 360 for 180)

Has anyone got a formula or any tips for this?


Solution

  • You can use MKMetersBetweenMapPoints to find dimensions in km.

    //Let's say region is represents the piece of map
        MKMapPoint pWest = MKMapPointForCoordinate( CLLocationCoordinate2DMake(region.center.latitude, region.center.longitude-region.span.longitudeDelta/2.0));
        MKMapPoint pEast = MKMapPointForCoordinate( CLLocationCoordinate2DMake(region.center.latitude, region.center.longitude+region.span.longitudeDelta/2.0));
        CLLocationDistance distW = MKMetersBetweenMapPoints(pWest, pEast)/1000.0;//map width in km
        MKMapPoint pNorth = MKMapPointForCoordinate( CLLocationCoordinate2DMake(region.center.latitude+region.span.latitudeDelta/2.0, region.center.longitude));
        MKMapPoint pSouth = MKMapPointForCoordinate( CLLocationCoordinate2DMake(region.center.latitude-region.span.latitudeDelta/2.0, region.center.longitude));
        CLLocationDistance distH = MKMetersBetweenMapPoints(pNorth, pSouth)/1000.0;;//map height in km