1

I'm experiencing exceptional behaviour from MapKit delegate

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation (id<MKAnnotation>)annotation
{
MKAnnotationView *annotationView=[mapView viewForAnnotation:annotation];
return annotationView;
}

When i add the annotation to mapkit and setdelegate to self this method is not called.But when i set the region like

 MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(coordinate, 7.5*1609.344 ,7.5*1609.344 );
[productsMapView setRegion:viewRegion animated:YES];

then delegate method is called.I dont know why this is happening.Some one help

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • I don't have an answer, but the method you posted is rather wrong ... `MKAnnotationView *annotaionView=[mapView viewForAnnotation:annotation];` is not correct – Volker Jun 05 '14 at 09:42
  • It is a mapView instance method, instead we can use [mapView dequeueReusableAnnotationViewWithIdentifier:@"productAnnotation"]; but the result is same. – user3710452 Jun 05 '14 at 10:06
  • What is your real question or problem? Why are you trying to figure out when viewForAnnotation is called? –  Jun 06 '14 at 02:31

1 Answers1

1

You need to consider the way framework works.

  1. viewForAnnotation method is only called when framework is about to draw the Annotation.
  2. Framework only draw an Annotation when the location (which contain that Annotation) is displayed.

So when you set you view to the region (which contains the annotation) framework invokes viewForAnnotation. (BTW, you have logical error in you viewForAnnotation method implementation)

Zee
  • 1,865
  • 21
  • 42
  • what is the logical error?,my doubt is annotation is drawn only when we set region around that?thanks for your reply – user3710452 Jun 06 '14 at 05:10
  • You are calling viewForAnnotation in viewForAnnotation method, it kind of recursion, instead you should use dequeueReusableAnnotationViewWithIdentifier, thats what I think. – Zee Jun 06 '14 at 07:59