How to suppress the “Current Location” callout in

2019-03-25 03:43发布

Tapping the pulsating blue circle representing the userLocation brings up a "Current Location" callout. Is there a way to suppress that?

6条回答
家丑人穷心不美
2楼-- · 2019-03-25 04:14

Swift 4

// MARK: - MKMapViewDelegate

func mapViewDidFinishLoadingMap(_ mapView: MKMapView) {
    if let userLocationView = mapView.view(for: mapView.userLocation) {
        userLocationView.canShowCallout = false
    }
}
查看更多
Animai°情兽
3楼-- · 2019-03-25 04:20

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
  }
  // ...
}
查看更多
唯我独甜
4楼-- · 2019-03-25 04:31

SWIFT Version

We need to set the user MKAnnotationView property canShowCallout to false when mapView adds the user MKAnnotationView

func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) {
    for view in views {
        if view.annotation is MKUserLocation {
            view.canShowCallout = false
        }
    }
}
查看更多
在下西门庆
5楼-- · 2019-03-25 04:32

I have two ways to help you:

  1. suppress in the mapViewDidFinishLoadingMap

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

    }

  2. suppress in the didUpdate

    func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
        //suppress the title
        mapView.userLocation.title = "My Location"
        //suppress other params
    }
    
查看更多
在下西门庆
6楼-- · 2019-03-25 04:33

You can set the title to blank to suppress the callout:

mapView.userLocation.title = @"";


Edit:
A more reliable way might be to blank the title in the didUpdateUserLocation delegate method:

-(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 title in the delegate methods lets you be certain you have a real userLocation instance to work with.

查看更多
Summer. ? 凉城
7楼-- · 2019-03-25 04:38

There's a property on the annotation view you can change, once the user location has been updated:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    MKAnnotationView *userLocationView = [mapView viewForAnnotation:userLocation];   
    userLocationView.canShowCallout = NO;
}    
查看更多
登录 后发表回答