简体   繁体   English

从mapView删除用户位置注释

[英]Remove user location annotation from mapView

I have to remove all the annotations added to my MKMapView but when I execute : 我必须删除添加到我的MKMapView的所有注释,但是在执行时:

NSMutableArray *annotationsToRemove = [[NSMutableArray alloc] initWithArray: mapView.annotations];
[mapView removeAnnotations: annotationsToRemove];

The array annotationsToRemove contains a MKUserLocation annotation and it doesn't delete it. 数组注释MKUserLocation包含一个MKUserLocation注释,它不会删除它。

Is there a way to reset the map? 有没有办法重置地图? I need to delete all the annotations from it! 我需要从中删除所有注释!

您可以将mapView的showsUserLocation属性设置为NO。

 mapView.showsUserLocation = NO;

Actually you can not edit MKUserLocation annotation. 实际上,您不能编辑MKUserLocation注释。 I mean you can not remove it from map annotation's array as it is a read-only property of MKMapView . 我的意思是您不能从地图注释的数组中删除它,因为它是MKMapViewread-only属性。

If you visit MKMapView.h class. 如果您访问MKMapView.h类。 You will find below line 您将在下面找到行

@property (nonatomic, readonly) MKUserLocation *userLocation;

Here we can see that this property is a read only. 在这里我们可以看到该属性是只读的。 So we can not delete it from MKMapView annotations array. 因此,我们无法从MKMapView批注数组MKMapView其删除。 How ever you face difficulties in calculation with other annotations then you can runtime hide it. 如果您在使用其他注释进行计算时遇到困难,那么您可以在运行时将其隐藏。

What I am trying to explain is when user location is not required any more you can set NO for user location property. 我要解释的是,当不再需要用户位置时,可以为用户位置属性设置NO

For example with your code: 例如,您的代码:

NSMutableArray *annotationsToRemove = [[NSMutableArray alloc] initWithArray: mapView.annotations];
[mapView removeAnnotations: annotationsToRemove];

[self.mapView setShowsUserLocation:NO];
NSLog(@"MapView annotations :%@", mapView.annotations);

Check NSLog output, you will see that MKUserLocation annotation is removed from mapView.annotations array. 检查NSLog输出,您将看到MKUserLocation批注已从mapView.annotations数组中删除。

It is the simple way I did follow. 这是我遵循的简单方法。 How ever I am not sure about there is other way to do this. 但是我不确定还有其他方法可以做到这一点。 Please leave a comment if you found any other solution. 如果找到其他解决方案,请发表评论。

In the h insert 在h插入

@property (weak)   MKAnnotationView *ulv;

In the m insert 在m插入

 - (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated {

        MKZoomScale currentZoomScale = mapView.bounds.size.width / mapView.visibleMapRect.size.width;
        NSLog(@"current zoom scale is %f",currentZoomScale);

        ulv = [mapView viewForAnnotation:mapView.userLocation];
                if( currentZoomScale > 0.049 ){
                    ulv.hidden = YES;
                }else{
                    ulv.hidden = NO;
                }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM