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?
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
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);
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 CLLocation
s at the center-left and center-right, then use -distanceFromLocation:
to get the distance between them in meters.
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