尝试遵循本教程:http ://www.switchonthecode.com/tutorials/building-an-earthquake-monitor-for-iphone-using-mapkit
我的图标显示在所有正确的位置(和正确的数字),并且图像也显示。(但它总是相同的图像。那是因为条件 if(self.marker.type == "nzpost" 不起作用,因为 type 始终为空(就像 initWithAnnotation() 中标记对象上的所有属性一样) . 由于某种原因,这些属性都是空的。
注解视图界面:
@interface PayMarkerAnnotationView : MKAnnotationView {
PaymentMarker *marker;
}
@property (strong, nonatomic) IBOutlet PaymentMarker *marker;
@end
带注释的初始化:(这里 self.marker.type 应该有一个值。
- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
if(self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {
self.backgroundColor = [UIColor clearColor];
NSString* imageName = [[NSString alloc] init];
if([self.marker.type isEqualToString:@"nzpost"]) {
imageName = @"icon-map-post.png";
} else {
imageName = @"icon-map-incharge.png";
}
self.image = [UIImage imageNamed:imageName];
}
return self;
}
地图视图:
- (MKAnnotationView *)mapView:(MKMapView *)lmapView viewForAnnotation:(id <MKAnnotation>)annotation {
PayMarkerAnnotationView *markerView = (PayMarkerAnnotationView *)[lmapView dequeueReusableAnnotationViewWithIdentifier:@"markerView"];
if(markerView == nil) {
markerView = [[PayMarkerAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"markerView"];
}
markerView.annotation = annotation;
return markerView;
}
添加我的标记
-(void)renderPaymentMarkers
{
[self.map addAnnotations: nzPostMarkers];
[self.map addAnnotations: inChargeMarkers];
}
Marker class:
@interface PaymentMarker : NSObject <MKAnnotation> {
NSString* type;
NSString* description;
float latitude;
float longitude;}
@property (nonatomic) NSString* description;
@property (nonatomic) NSString* type;
@property (nonatomic) float latitude;
@property (nonatomic) float longitude;
//MKAnnotation
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@end