用于用户位置 pin iPhone 的 MKAnnotationView
我有一个使用 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
用于显示当前位置的类,但因为它不是公开的,所以使用起来不安全,而且我的应用程序不断崩溃,我确信有一种更简单的方法。
是否可以做我想做的事,或者我不应该删除地图视图的用户位置注释?
提前致谢
I have an application that uses MKMapView and at some point in the app I need to remove all the current pins in the map using
[mapView removeAnnotations:mapView.annotations]
And then I want to show again the current user location
mapView.showUserLocation = YES
But I can only make it reappear as a regular pin, because the userLocation view class its not public so I cant return views of that type. Here is my code
- (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;
}
Also I have found through reflection that
MKUserLocationView
is the class that is used to display the current location, but because its not public its not safe to use and my app keeps crashing and Im sure theres a easier way.
Is it possible to do what I want, or should I just never remove the user location annotation of the mapView?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您只想显示用户位置的蓝点而不是常规图钉,请在此 mapView 委托函数中返回 nil。或者返回您自己的 MKAnnotationView (子类)对象:
If you only want to show a blue dot for the user's location in stead of a regular pin, return nil in this mapView delegate function. Alternatively return your own MKAnnotationView (subclass) object:
尝试使用 NSMutableArray 来实现。希望这可以帮助您解决您的问题!
Try doing it with an NSMutableArray. Hope this helps you solving your problem!