6

like the title, does any one know how to calculate size width to meters in every region, means in every where and any zoom.

I found how to get the zoomScale.

CLLocationDegrees longitudeDelta = myMapView.region.span.longitudeDelta;
CGFloat mapWidthInPixels = myMapView.bounds.size.width;
double zoomScale = longitudeDelta * MERCATOR_RADIUS * M_PI / (180.0 * mapWidthInPixels);

But I don't know what does result mean, if I want to get the meters for size width or maybe a line 20f, I change the mapWidthInPixels to 20 does it right?

Wayn Liu
  • 394
  • 3
  • 13

3 Answers3

8

You can find an elaborate explanation of the formula here: transform longitude latitude into meters.

Sample code:

CLLocationDegrees deltaLatitude = self.mapView.region.span.latitudeDelta;
CLLocationDegrees deltaLongitude = self.mapView.region.span.longitudeDelta;
CGFloat latitudeCircumference = 40075160 * cos(self.mapView.region.center.latitude * M_PI / 180);
NSLog(@"x: %f; y: %f", deltaLongitude * latitudeCircumference / 360, deltaLatitude * 40008000 / 360);

Credits to Mark Ransom

Community
  • 1
  • 1
fguchelaar
  • 4,779
  • 23
  • 36
7

This worked great for me. My solution is in Swift however the Obj-C is similar.

Swift :

let mRect: MKMapRect = self.mapView.visibleMapRect
        let eastMapPoint = MKMapPointMake(MKMapRectGetMinX(mRect), MKMapRectGetMidY(mRect))
        let westMapPoint = MKMapPointMake(MKMapRectGetMaxX(mRect), MKMapRectGetMidY(mRect))
        let currentDistWideInMeters = MKMetersBetweenMapPoints(eastMapPoint, westMapPoint)
        let milesWide = currentDistWideInMeters / 1609.34  // number of meters in a mile
        println(milesWide)

Obj-C (compliments to the original contributor of the below code https://stackoverflow.com/a/5813609/3750109)

MKMapRect mRect = self.mapView.visibleMapRect;
        MKMapPoint eastMapPoint = MKMapPointMake(MKMapRectGetMinX(mRect), MKMapRectGetMidY(mRect));
        MKMapPoint westMapPoint = MKMapPointMake(MKMapRectGetMaxX(mRect), MKMapRectGetMidY(mRect));

        self.currentDistWideInMeters = MKMetersBetweenMapPoints(eastMapPoint, westMapPoint);
Community
  • 1
  • 1
Christopher Wade Cantley
  • 7,122
  • 5
  • 35
  • 48
1

The distance in longitudinal meters will change as latitude increases. The same longitude value becomes smaller in meters as you go further from the equator. The best way would probably be to create two CLLocations at the center-left and center-right, then use -distanceFromLocation: to get the distance between them in meters.

nevan king
  • 112,709
  • 45
  • 203
  • 241