我有一个使用MKMapView的应用程序,在应用程序中的某个点上,我需要使用移除地图中的所有当前图钉
[mapView removeAnnotations:mapView.annotations]然后,我想再次显示当前用户的位置
mapView.showUserLocation = YES但是我只能让它作为一个普通的别针重新出现,因为userLocation视图类不是公共的,所以我不能返回该类型的视图。以下是我的代码
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id<MKAnnotation>)annotation
MKPinAnnotationView* annView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"currentloc"];
if (!annView) {
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
annView.pinColor = MKPinAnnotationColorRed;
annView.animatesDrop=TRUE;
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5, 5);
if ([annotation isKindOfClass:[DraggableAnnotationAM class]] ) annView.draggable =YES;
return annView;
}
else {
if ([annotation isKindOfClass:[DraggableAnnotationAM class]] ) annView.draggable = YES;
annView.annotation = annotation;
return annView;
}我也通过反思发现,
MKUserLocationView是用于显示当前位置的类,但因为它不是公共的,所以使用它不安全,而且我的应用程序总是崩溃,我相信有更简单的方法。
有没有可能做我想做的事情,或者我永远不应该删除mapView的用户位置注释?
提前感谢
发布于 2011-01-02 09:38:16
尝试使用NSMutableArray来完成此操作。希望这能帮助你解决你的问题!
NSMutableArray *annotation = [[NSMutableArray alloc] init];
for (id <MKAnnotation> annot_ in [mapView annotations])
{
if ( [annot_ isKindOfClass:[MKUserLocation class]] ) {
}
else {
[annotation addObject:annot_];
}
}
[mapView removeAnnotations:annotation];
[annotation release];
annotation = nil;发布于 2017-01-19 06:16:00
如果您只想显示用户位置的蓝点,而不是常规的别针,则在此mapView委托函数中返回nil。或者返回您自己的MKAnnotationView (子类)对象:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil // or return a custom MKAnnotationView
} else { // etc.https://stackoverflow.com/questions/4568148
复制相似问题