On mkuserlocation, how do I show my own custom message in viewForAnnotation

可紊 提交于 2019-12-20 05:49:16

问题


I want to show a custom message instead of "My Location" in viewForAnnotation. How do I do this?

Thanks Deshawn


回答1:


In the delegate of your MKMapView, implement the method mapView:viewForAnnotation and check if the annotation is of type MKUserLocation. If yes, change the title and subtitle properties of the annotation. The callout will automatically pull the new values. Or you can create a totally new view and return it here.

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

Disclaimer: I haven't tested this code.




回答2:


It can be done by updating Title property of MKUserLocation.

As MKAnnotation protocol doesn't require making Title a property, cast annotation passed as an argument to MKUserLocation and set the property

- (MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:    (id<MKAnnotation>)annotation {
    if ([annotation isKindOfClass:[MKUserLocation class]]) {
        [(MKUserLocation*)annotation setTitle: @"I am here"];
        return nil;
    }
    return nil;
}



回答3:


Simply referring directly to it works too, like this...

mapView.userLocation.title = @"I am here";

This can be done from anywhere you have a reference to the map view.



来源:https://stackoverflow.com/questions/6400880/on-mkuserlocation-how-do-i-show-my-own-custom-message-in-viewforannotation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!