开发者

MKAnnotationView for userLocation pin iPhone

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


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.


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜