How to stop the viewForAnnotation method from overriding the default user location blue beacon in iOS

前端 未结 2 574
说谎
说谎 2020-12-15 06:30

Right now I am populating a map view with annotations, and also displaying the user\'s current location. With the viewForAnnotation method, it overrides all annotations with

相关标签:
2条回答
  • 2020-12-15 07:10

    Check out the documentation here:

    http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapViewDelegate_Protocol/MKMapViewDelegate/MKMapViewDelegate.html

    As it states:

    If the object in the annotation parameter is an instance of the MKUserLocation class, you can provide a custom view to denote the user’s location. To display the user’s location using the default system view, return nil.

    So you can add a conditional to check for this:

    if([annotation isKindOfClass: [MKUserLocation class]]) {
      return nil;
    }
    
    0 讨论(0)
  • 2020-12-15 07:11

    Swift 5.0

    Put a check at the top of the function to ignore the userLocation:

       func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
            guard annotation as? MKUserLocation != mapView.userLocation else { return }
    
    0 讨论(0)
提交回复
热议问题