0

I am trying to create a function that takes in a parameter of meters and returns a pixel width for an MKMapView.

How can I correctly determine from the current zoom of the map, how many pixels represent the correct amount of meters?

So an example is let's say I have a zone radius of 78 meters, and the current distance across the map is 6000 meters. How can I represent those 78 meters as a scaled proportional UIView on the map?

My plan was to first calculate how many meters across the map are currently being represented. So here is what I have for that. But I am getting stuck on trying to figure out how to convert the amount of meters into the correct pixel widths. I already have the correct zone radiuses as well.

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    MKMapRect currentRect = mapView.visibleMapRect;
    MKMapPoint farEastPoint = MKMapPointMake(MKMapRectGetMinX(currentRect), MKMapRectGetMidY(currentRect));
    MKMapPoint farWestPoint = MKMapPointMake(MKMapRectGetMaxX(currentRect), MKMapRectGetMidY(currentRect));
    double currentDis =MKMetersBetweenMapPoints(farEastPoint, farWestPoint);
}
Luke
  • 612
  • 1
  • 6
  • 19
  • 1
    See http://stackoverflow.com/questions/8412088/convert-distance-on-mkmapview-to-distance-for-uiview. But why not add an overlay like an MKCircle to the map view instead (the map view will then take care of scaling and moving the zone region for you)? –  Jun 01 '14 at 15:19
  • Because I am trying to add animations to the view – Luke Jun 01 '14 at 17:46
  • Then the `convertRegion:toRectToView:` method as the linked answer mentions will help. Just be sure to specify the proper view for the ToView: parameter. If your UIView is a subview of the map's parent VC (eg. self.view), then ToView: should be self.view. I don't recommend adding a subview to the map view directly. –  Jun 01 '14 at 21:02
  • 1
    What I decided to do was create a map annotation and have subview on the map annotation. I got the correct width I needed by using MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(map.centerCoordinate,radius.floatValue,radius.floatValue); CGRect myRect = [map convertRegion: region toRectToView: nil]; – Luke Jun 01 '14 at 21:04

0 Answers0