I am drawing lines on an MkMapView and I want to specify their width in meters.
I tried something like this:
-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id <MKOverlay>) overlay
{
if([overlay isKindOfClass:[MKPolyline class]])
{
MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
double ppm = MKMapPointsPerMeterAtLatitude(myLatitude);
renderer.lineWidth = ppm * myMetersValue;
...
This produces a line that is much wider than myMetersValue and it does not scale when I zoom in and out.
ppm remains constant at approx. 9.49 even when I zoom the map in and out. I have verified myLatitude to be a valid lat (approx. 45 degrees). I have verified myMetersValue to be a reasonable # (approx. 18m).
(edit)
I have discovered that the MapPoints returned by MKMapPointsPerMeterAtLatitude are not screen points, so I can't use that function.
So my basic problem is how to convert meters to screen points in the mkMapView. More succinctly, how do I get a value for ppm in my code? I tried this (found elsewhere on SO):
MKCoordinateRegion myRegion = MKCoordinateRegionMakeWithDistance(mapView.centerCoordinate, 400, 0);
CGRect myRect = [mapView convertRegion: myRegion toRectToView: nil];
double ppm = myRect.size.width / 400.0;
That did not work.
Then I tried this:
CLLocationCoordinate2D l1 = [mapView convertPoint:CGPointMake(0,0) toCoordinateFromView:mapView];
CLLocation *ll1 = [[CLLocation alloc] initWithLatitude:l1.latitude longitude:l1.longitude];
CLLocationCoordinate2D l2 = [mapView convertPoint:CGPointMake(0,500) toCoordinateFromView:mapView];
CLLocation *ll2 = [[CLLocation alloc] initWithLatitude:l2.latitude longitude:l2.longitude];
double ppm = 500.0 / [ll1 distanceFromLocation:ll2];
The idea here is to get the lat/lon for two points that are 500 screen points apart. Then get the distance in meters between them so I can calculate ppm. This sort of worked, but it looks like the resulting ppm is not quite right so I am not convinced that this is correct. Also, when I zoom the map the existing lines on the map are not re-rendered so the stay the same width after the zoom (but that is a different problem).
(edit)
It now looks like my calculation of ppm is correct. The problem is that the renderer is called before the user finishes zooming so the lines are not rendered at the final zoom scale. When I force re-rendering once the final zoom scale is determined then ppm is correct and my code works.