(Revised Question based on comment:)
OK I will try to ask in other way...How can I get border coordinates of this circle overlay:
(Original Question:)
I am having weird problem with my iPhone app. I have MKCoordinateRegion which has center coordinate latitude: 51.509980 and longitude: -0.133700. I have used method MKCoordinateRegionMakeWithDistance and set the distance to be 16,093.44 meters (10 miles).
I want to get border point of this region therefore I have this code:
MKCoordinateRegion region2 = self.myMapView.region;
float minLat = region2.center.latitude - (region2.span.latitudeDelta / 2.0);
float maxLat = region2.center.latitude + (region2.span.latitudeDelta / 2.0);
float minLong = region2.center.longitude - (region2.span.longitudeDelta / 2.0);
float maxLong = region2.center.longitude + (region2.span.longitudeDelta / 2.0);
I found this website for my testing http://boulter.com/gps/distance/ which calculates distance between two coordinates. When I enter as a FROM coordinate: 51.509980 and longitude: -0.133700 (London) and TO coordinate:
2011-11-26 01:15:42.830 NearMeTest[3911:11603] MinLAT 51.334381 and MaxLAT 51.684814
2011-11-26 01:15:42.830 NearMeTest[3911:11603] MinLONG -0.352936 and MaxLONG 0.086517
I get that the distance between those two coordinates is 15.40 miles instead of expected 10 miles.
Screenshot:
Why such a difference? When I tried to do the same but from different center coordinates (Tokyo, New York) result were correct 10 miles.
Thanks for reply
(Revised Answer based on comment:)
If you mean you want the coordinates of the bounding rectangle for that circle overlay, use the overlay's boundingMapRect
property:
//"theCircle" is the MKCircle overlay object
CLLocationCoordinate2D topLeftCoord =
MKCoordinateForMapPoint(theCircle.boundingMapRect.origin);
MKMapPoint bottomRightMapPoint = MKMapPointMake (
MKMapRectGetMaxX(theCircle.boundingMapRect),
MKMapRectGetMaxY(theCircle.boundingMapRect));
CLLocationCoordinate2D bottomRightCoord =
MKCoordinateForMapPoint(bottomRightMapPoint);
(Original Answer:)
First, when you call setRegion
on the map view, the map view will almost always modify the region you requested so that it fits in the map view. This adjustment is based on the shape of the map view and whether it can show the requested span properly at one of its fixed zoom levels.
For example, if your map view is not square and you ask for a span of 10 miles in both directions, at least one of the spans is definitely going to be adjusted. Even if you ask for a span that you've set based on the proportions of the view, it could still get adjusted if the map view can't show the tiles at that zoom level (or perhaps if you've not taken the Earth's curvature into account).
Next, the latitudeDelta
and longitudeDelta
define the entire height and width of the region (not the distance from the center coordinate).
So your test in the screenshot cannot be compared with the span delta. In the screenshot, you are calculating the distance from the center coordinate to the minimum latitude and longitude (bottom-left corner) but the span deltas go all the way from right to left and bottom to top. (Because of that, you'd think that the distance from the center to a corner should be less than the delta--not more. It is shorter but the delta has also increased to more than 10 because of the reasons described above.)
Finally, to get the corner coordinates (bottom-left and top-right), this is probably a more accurate way to do it:
CLLocationCoordinate2D bottomLeftCoord =
[myMapView convertPoint:CGPointMake(0, myMapView.frame.size.height)
toCoordinateFromView:myMapView];
CLLocationCoordinate2D topRightCoord =
[myMapView convertPoint:CGPointMake(myMapView.frame.size.width, 0)
toCoordinateFromView:myMapView];
In your screenshot of the web form, you're getting a different distance because the line you're measuring (center to MinLAT / MinLONG) is diagonal and extends beyond the circle radius.
If you follow @AnnaKarenina's answer, you can then get the distance in meters by converting each CLLocationCoordinate2D
into a CLLocation
.
Here's how you'd get the radius in miles:
CLLocation * centerLeftLocation = [[CLLocation alloc]
initWithLatitude:centerCoord.latitude
longitude:topLeftCoord.longitude];
CLLocation * centerLocation = [[CLLocation alloc]
initWithLatitude:centerCoord.latitude
longitude:centerCoord.longitude];
CLLocationDistance distanceInMeters = [centerLeftLocation distanceFromLocation:centerLocation];
float distanceInMiles = distanceInMeters / 1609.344f;
[centerLeftLocation release];
[centerLocation release];