【发布时间】:2014-08-07 14:10:42
【问题描述】:
这听起来像是一个笼统的问题,但我只有在我想要我的所有图钉都使用相同的图像时才找到答案,而我不需要。
这就是我现在的工作方式:
我的所有位置都在一个 Locations 数组中(具有长、纬度、名称、引脚名称的自定义类)。
在 viewdidLoad 中,我循环该数组并使用找到的每个对象创建我的引脚,请参见以下代码:
for(int i = 0 ; i<[_locationList count] ; i++)
{
Location *item = [_locationList objectAtIndex:i];
CLLocationCoordinate2D coordinate;
coordinate.latitude = item.locationLatitude;
coordinate.longitude = item.locationLongitude;
NSString *title = item.locationName;
CustomAnnotation* ann = [CustomAnnotation new];
ann.name = title;
ann.coordinate = coordinate;
ann.pinName = [NSString stringWithFormat:@"pin%i.png",item.idPin];
[self.map addAnnotation:ann];
}
这很简单,来自 CustomAnnotation 类,代码如下:
@interface CustomAnnotation : MKPointAnnotation <MKAnnotation>{
}
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *description;
@property (nonatomic, retain) NSString *pinName;
@end
这都是我在互联网上看到的东西,我有点相信到目前为止都是正确的。
在我看来,我仍在创建非常经典的 pin,它们只是多了一个属性 (pinName),这就是它来自自定义类的原因。
现在,在 viewForAnnotation 中,我完全不知道如何告诉它获取该 pinName 并使用它。
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
// If it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
// Handle any custom annotations.
if ([annotation isKindOfClass:[MKPointAnnotation class]])
{
// Try to dequeue an existing pin view first.
MKAnnotationView *pinView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];
if (!pinView)
{
// If an existing pin view was not available, create one.
pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];
pinView.canShowCallout = YES;
pinView.image = // I wanted to write annotation.pinName But it's just not that.
pinView.calloutOffset = CGPointMake(0, 32);
}else {
pinView.annotation = annotation;
}
return pinView;
}
return nil;
}
我错过了什么?我显然做错了什么,但我无法弄清楚,我仍然对 MKAnnotationsView & MKPointAnnotation & MKPinAnnotation 之间的区别感到很困惑,...
更多信息:引脚名称是“pinX.png”,X 是 1 到 12 之间的数字。我只想使用该名称,以便程序可以在图片所在的资源中找到它。
【问题讨论】:
标签: ios mapkit mkannotationview