Oksana
Oksana

Reputation: 13952

Convert distance on MKMapView to distance for UIView

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.

Upvotes: 2

Views: 3213

Answers (2)

Denis
Denis

Reputation: 6413

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.

Upvotes: 10

yonel
yonel

Reputation: 7865

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.

Upvotes: 5

Related Questions