用于用户位置 pin iPhone 的 MKAnnotationView

发布于 2024-10-10 00:03:48 字数 1281 浏览 0 评论 0原文

我有一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

新人笑 2024-10-17 00:03:48

如果您只想显示用户位置的蓝点而不是常规图钉,请在此 mapView 委托函数中返回 nil。或者返回您自己的 MKAnnotationView (子类)对象:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        if annotation is MKUserLocation {
            return nil // or return a custom MKAnnotationView
        } else { // etc.

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:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        if annotation is MKUserLocation {
            return nil // or return a custom MKAnnotationView
        } else { // etc.
最后的乘客 2024-10-17 00:03:48

尝试使用 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;

Try doing it with an NSMutableArray. Hope this helps you solving your problem!

 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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文