2

How do I convert distance (for example 400 meters) on MKMapView to distance for UIView? I want to show MKAnnotationView that depends on current zoom level on MKMapView.

Mikayil Abdullayev
  • 12,117
  • 26
  • 122
  • 206
Oksana
  • 13,442
  • 10
  • 53
  • 89

2 Answers2

10

First, create a region with 400 meters length:

MKCoordinateRegion myRegion = MKCoordinateRegionMakeWithDistance(mapView.centerCoordinate, 400, 0);

After that use convert region methods for getting real rect:

CGRect myRect = [mapView convertRegion: myRegion toRectToView: nil];
NSLog(@"width for 400m is %f", myRect.size.width);

Hope, this helps.

Denis
  • 6,313
  • 1
  • 28
  • 27
  • 1
    if you set the distance as the `latitudinalMeters` attribute for `MKCoordinateRegionMakeWithDistance`, as in your example, you should really read the height not the width of the resulting `CGRect`. See http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MapKitFunctionsReference/Reference/reference.html#//apple_ref/c/func/MKCoordinateRegionMakeWithDistance – Nicu Surdu Nov 01 '12 at 12:39
5

You have several selectors in MKMapView to convert UIView coordinates on the screen to geo coordinates (and geo -> screen as well):

- (CGPoint)convertCoordinate:(CLLocationCoordinate2D)coordinate toPointToView:(UIView *)view
- (CLLocationCoordinate2D)convertPoint:(CGPoint)point toCoordinateFromView:(UIView *)view

I assume 400 meters is a distance between 2 coordinates, so should you have a reference to 2 CLLocationCoordinate2D structures.
Just use - (CGPoint)convertCoordinate:(CLLocationCoordinate2D)coordinate toPointToView:(UIView *)view to get the corresponding CGPoint (= screen coordinates) and you should be then able to calculate the screen distance.

yonel
  • 7,855
  • 2
  • 44
  • 51