Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS how to calculate size width to meters in every region(any location and any zoom)

Tags:

ios

mkmapview

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?

like image 535
Wayn Liu Avatar asked Dec 20 '12 08:12

Wayn Liu


3 Answers

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

like image 156
fguchelaar Avatar answered Nov 13 '22 00:11

fguchelaar


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);
like image 7
Christopher Wade Cantley Avatar answered Nov 13 '22 02:11

Christopher Wade Cantley


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.

like image 1
nevan king Avatar answered Nov 13 '22 01:11

nevan king