1

I'm trying to add the distance from the user's position to a selected annotation's subtitle in a mapview. The mechanics of it are working, but the actual callout gets messed up the first time it's displayed. There appears to be a redraw problem.

mapView annotation redraw problem

Subsequent taps on the pin show the correct layout.

Here's the relevant code:

// called when selecting annotations

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{

MKPointAnnotation *selectedAnnotation = view.annotation;

//attempt to add distance on annotation
CLLocation *pointALocation = [[CLLocation alloc] 
                              initWithLatitude:selectedAnnotation.coordinate.latitude 
                              longitude:selectedAnnotation.coordinate.longitude];
float distanceMeters = [pointALocation distanceFromLocation:locationManager.location];

//for sending info to detail
myPinTitle = selectedAnnotation.title;

[selectedAnnotation setSubtitle:[NSString stringWithFormat:@"%.2f miles away", (distanceMeters / 1609.344)]];

} I've tried calling [view setNeedsDisplay], but to no avail.

Thanks in advance for your help.


The Solution that Worked

Here's the solution I finally came up with. It seems to work.

I edited out the duplicate code from the didSelectAnnotationView method, above, and came up with:

//called when user location changes

- (void)updatePinsDistance
{
for (int x=0; x< [[mapView annotations]count]; x++) {
    MKPointAnnotation *thisPin =[[mapView annotations] objectAtIndex:x];

    //attempt to add distance on annotation
    CLLocation *pointALocation = [[CLLocation alloc] 
                                 initWithLatitude:thisPin.coordinate.latitude 
                                 longitude:thisPin.coordinate.longitude];
    float distanceMeters = [pointALocation distanceFromLocation:locationManager.location];
    NSString *distanceMiles = [NSString stringWithFormat:@"%.2f miles from you",
                                                    (distanceMeters / 1609.344)];

    [thisPin setSubtitle:distanceMiles];
    }
}

1 Answer 1

3

You should set your subtitle in another place than didSelectAnnotationView. Actually all annotationViews should have their title and subtitle set before they are returned by the mapView:viewForAnnotation: method.

The fact that you set a long subtitle certainly explains that the callout is not the right size. The size must be calculated before the didSelectAnnotationView is called.

5
  • The original subtitle is added when the annotations are created, but I want to show the distance from the current user's location to the selected point, so I can't add that info then. Also, the replaced distance string may actually be smaller than the original one. Apr 9, 2012 at 18:05
  • Instead, you should recalculate it when the user location changes using the mapView:didUpdateUserLocation: delegate method. Frankly, changing the subtitle while showing the callout is not a reasonable thing to do. Apr 9, 2012 at 20:42
  • So, should I change the subtitle of every annotation when the user's position changes, even if it'll never be seen, rather than just the one that needs to be displayed? I'm confused as how that's more reasonable. Can you elaborate? Thanks. Apr 9, 2012 at 22:14
  • As I told you, the title and subtitle should be set before the annotation view is selected. You modifying it afterwards in the didSelectAnnotationView is the most probable cause that the callout is not well redrawn: all the sizes are calculated before the callout is displayed. On the other side, setting the subtitle is not an expensive task, and you can always put this in another thread using dispatch_async. Changing something while displaying it is not "reasonable" ;-) Apr 10, 2012 at 10:19
  • Thanks. This helped. Please see my somewhat convoluted (but working) solution edited in up above. Apr 10, 2012 at 18:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.