iPhone SDK:MapKit 多个自定义注解 [英] iPhone SDK: MapKit multiple custom annotations
问题描述
我能够将大量位置列表加载到我的 MapKit 中,并使用自定义 Pin 图和注释显示它们.
I am able to load a big list of location onto my MapKit and display them all with a custom Pin image and annotation.
我遇到的问题是我目前的所有注释都显示相同的标题、副标题和 pinImage.
The issue i'm having is that I currently have all annotations displaying the same Title, Subtitle, and pinImage.
如何制作它以便我可以使用自己的标题和不同的 Pin 图设置每个注释?我很难通过 mapView:viewForAnnotation 确定正在设置的注释.
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;
}
推荐答案
您需要将 annotation 对象强制转换为您的自定义类型并访问它的属性以适当地更改您返回的 annotationView.
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 的 WeatherMap 示例项目是如何做到这一点的一个很好的例子.https://developer.apple.com/library/ios/#samplecode/WeatherMap/Introduction/Intro.html
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
MapCallouts 示例项目是相同的想法,但实现更简单.https://developer.apple.com/library/ios/#samplecode/MapCallouts/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
这篇关于iPhone SDK:MapKit 多个自定义注解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!