viewForAnnotation没有被触发

时间:2011-06-28 21:12:17

标签: ios mapkit

我在地图上显示一个图钉,但我无法自定义注释视图的显示。由于某种原因,我的viewForAnnotation没有被调用。这是didFinishLaunchingWithOptions方法。

- (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;


}

这是我永远不会被调用的viewForAnnotation方法。

- (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 imageNamed:@"house.png"]];
    pinView.leftCalloutAccessoryView = houseIconView; 
    [houseIconView release]; 

    return pinView; 
}

这里是didUpdateUserLocation方法:

- (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];

}

1 个答案:

答案 0 :(得分:8)

viewForAnnotation委托方法必须命名为mapView:viewForAnnotation:

您的方法名为mv:viewForAnnotation:

修改
以下是我的评论中建议的另外两个更改的示例:

- (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; 
}