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

前端 未结 3 1620
灰色年华
灰色年华 2021-01-28 06:09

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

Thanks Deshawn

3条回答
  •  遇见更好的自我
    2021-01-28 06:59

    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 )annotation {
        if ([annotation isKindOfClass:[MKUserLocation class]]) {
            annotation.title = @"I am here";
            return nil;
        }
        return nil;
    }
    

    Disclaimer: I haven't tested this code.

提交回复
热议问题