How to suppress the current location leader in a map view

Clicking on the pulsating blue circle representing the user area brings up the Current Location callout. Is there any way to suppress this?

+11
source share
5 answers

In the annotation window, you can change the property after changing the user's location:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation { MKAnnotationView *userLocationView = [mapView viewForAnnotation:userLocation]; userLocationView.canShowCallout = NO; } 
+15
source

You can set title to empty to suppress a leader:

 mapView.userLocation.title = @""; 


Edit:
A more reliable way would be to remove the header in the didUpdateUserLocation delegate didUpdateUserLocation :

 -(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation { userLocation.title = @""; } 

or in viewForAnnotation :

 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>) annotation { if ([annotation isKindOfClass:[MKUserLocation class]]) { ((MKUserLocation *)annotation).title = @""; return nil; } ... } 

Setting the header in the delegate methods allows you to make sure that you have a real instance of userLocation to work with.

+12
source

Swift 4 - Xcode 9.2 - iOS 11.2

 // MARK: - MKMapViewDelegate func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { if let userLocation = annotation as? MKUserLocation { userLocation.title = "" return nil } // ... } 
+1
source

I have two ways to help you:

  • suppress in mapViewDidFinishLoadingMap

     func mapViewDidFinishLoadingMap(_ mapView: MKMapView) { mapView.showsUserLocation = true //suppress the title mapView.userLocation.title = "My Location" //suppress other params 

    }

  • suppress in didUpdate file

     func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) { //suppress the title mapView.userLocation.title = "My Location" //suppress other params } 
0
source

Swift 4

 // MARK: - MKMapViewDelegate func mapViewDidFinishLoadingMap(_ mapView: MKMapView) { if let userLocationView = mapView.view(for: mapView.userLocation) { userLocationView.canShowCallout = false } } 
0
source

Source: https://habr.com/ru/post/1394149/


All Articles