我将一个完美运行的 6.1 应用程序升级到了 Xcode 5/ios 7 使用 MKMapKit 在 map View 上显示一些图钉。
现在注释图钉不显示了。我确定 我的 mapview 委托(delegate)是正确的,正在调用 viewForAnnotation。 我也确定它的标题和副标题已设置,但现在 pin 只是没有 展示。除了这个之外,还有人知道要寻找什么吗?
当它运行时,我看到我的纬度/经度、图钉标题和副标题已填好。
这是我的代码:
- (MKAnnotationView *) mapView:(MKMapView *) theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
{
return nil;
}
[self.mapView removeAnnotations:self.mapView.annotations];
MKPointAnnotation *pt = (MKPointAnnotation *)annotation;
NSString *PINNAME = pt.title;
CLLocationDegrees lat = pt.coordinate.latitude;
CLLocationDegrees lon = pt.coordinate.longitude;
[Log log:TINFO :@"lat/lon %f - %f", lat, lon];
[Log log:TINFO :@"pin title userid = %@", pt.title];
[Log log:TINFO :@"subtitle: %@", pt.subtitle];
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier: PINNAME];
if(pinView == nil)
{
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:PINNAME];
pinView.pinColor = MKPinAnnotationColorGreen;
}
else
{
pinView.annotation = annotation;
}
return pinView;
}
最佳答案
您应该从 viewForAnnotation
中删除对 removeAnnotations
的调用,因为将它放在那里会删除您可能已经在 map 上显示的任何注释。如果您的意图是在添加新注释之前删除旧注释,您应该在调用 addAnnotation
之前执行此操作,而不是在 viewForAnnotation
中执行此操作。
与您当前的问题无关:
即使您真的想删除所有注释,如果您可能(在某个时候)在 map 上有
MKUserLocation
,我建议只删除注释类不是MKUserLocation
的注释。当我不小心从annotations
数组中删除了MKUserLocation
时,我注意到了奇怪的行为。顺便说一句,我可能建议使用
theMapView
参数,而不是self.mapView
。您不想冒因困惑的IBOutlet
而导致问题的风险。当theMapView
作为参数传递时,无需引用self.mapView
。可以想象,您正在为每个
MKAnnotationView
使用唯一的identifier
。我在这段代码中没有看到任何需要这样做的东西,并且您会失去使用出列注释所获得的大部分效率。对特定类型的所有注释 View 使用相同的identifier
字符串(并且您似乎只有一种类型的注释)。
关于ios 7 pin viewforannotation 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19194269/