viewForAnnotation not being fired
I am displaying a pin on the map but I am not able to customize the display of the annotation view. For some reason my viewForAnnotation is not being called. Here is didFinishLaunchingWithOptions method.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[mapView setDelegate:self];
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[mapView setShowsUserLocation:YES];
// Override point for customization after application launch.
[self.window makeKeyAndVisible];
return YES;
}
And here is my viewForAnnotation method which is never being called.
- (MKAnnotationView *)mv:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
NSLog(@"viewForAnnotation");
if([annotation isKindOfClass:[MKUserLocation class]])
return nil;
static NSString *annotationIdentifier = @"AnnotationIdentifier";
MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];
[pinView setPinColor:MKPinAnnotationColorGreen];
pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
UIImageView *houseIconView = [[UIImageView alloc] initWithImage:[UIImage imageNa开发者_Python百科med:@"house.png"]];
pinView.leftCalloutAccessoryView = houseIconView;
[houseIconView release];
return pinView;
}
and here is didUpdateUserLocation method:
- (void)mapView:(MKMapView *)mv didUpdateUserLocation:(MKUserLocation *)userLocation
{
NSTimeInterval t = [[[userLocation location] timestamp] timeIntervalSinceNow];
if(t < -180) return;
NSLog(@"%@",[textField text]);
MapPoint *mp = [[MapPoint alloc] initWithCoordinate:userLocation.location.coordinate title:[textField text]];
[mv addAnnotation:mp];
[mp release];
}
The viewForAnnotation
delegate method must be named mapView:viewForAnnotation:
.
Your method is named mv:viewForAnnotation:
.
Edit:
Here is an example with the two other changes suggested in my comments:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
NSLog(@"viewForAnnotation");
if([annotation isKindOfClass:[MKUserLocation class]])
return nil;
static NSString *annotationIdentifier = @"AnnotationIdentifier";
MKPinAnnotationView *pinView = (MKPinAnnotationView *) [mapView
dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
if (!pinView)
{
pinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation
reuseIdentifier:annotationIdentifier] autorelease];
[pinView setPinColor:MKPinAnnotationColorGreen];
pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
UIImageView *houseIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"house.png"]];
pinView.leftCalloutAccessoryView = houseIconView;
[houseIconView release];
}
else
{
pinView.annotation = annotation;
}
return pinView;
}
精彩评论