iPhone SDK: MapKit multiple custom annotations

耗尽温柔 提交于 2019-12-09 22:09:39

问题


I am able to load a big list of location onto my MapKit and display them all with a custom Pin image and annotation.

The issue i'm having is that I currently have all annotations displaying the same Title, Subtitle, and pinImage.

How do I make it so that I can set each annotation with its own Title and different Pin image? I'm having a hard time trying to identify what annotation is being setup via mapView:viewForAnnotation.

- (NSString *)subtitle{

    return @"This is the annotation subtitle.";
}

- (NSString *)title{
    return @"Annotations Title";
}

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    if (annotation == mapView.userLocation) {
        return nil;
    }

    MKAnnotationView *annView=[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
    annView.canShowCallout = YES;
    annView.calloutOffset = CGPointMake(-5, 5);
    UIImage *pinImage = [UIImage imageNamed:@"mapPin.png"];
    annView.image = pinImage;
    return annView;
}

回答1:


You'll need to cast the annotation object to your custom type and access it's properties to appropriately change the annotationView that you return.

Apple's WeatherMap sample project is a good example of how to do this. https://developer.apple.com/library/ios/#samplecode/WeatherMap/Introduction/Intro.html

The MapCallouts sample project is the same idea, but a simpler implementation. https://developer.apple.com/library/ios/#samplecode/MapCallouts/Introduction/Intro.html



来源:https://stackoverflow.com/questions/3800660/iphone-sdk-mapkit-multiple-custom-annotations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!