iOS – How to make MKUSERLOCATION?

Title and subtitle can be added to the user location displayed by iOS using MKUserLocation. When the user clicks on the location, these titles and subtitles will be displayed in the bubble above the location. By using setSelected: animated: Select a comment from MKAnnotationView, you can display the thought bubble of other comments. Unfortunately, MKUserLocation does not come from MKAnnotationView.

How to programmatically select the user location so that the comment is displayed on the user location Without requiring the user to click on it first?

MKAnnotationView’s documentation explains its setSelected: animated: method (and similar Content):

You should not call this method directly.

Instead, use the MKMapView method selectAnnotation: animated :. If you call it in the didAddAnnotationViews delegate method, you can ensure that the annotation view is ready to display annotations, otherwise calling selectAnnotation will not perform any operation.

For example:

-(void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
for (MKAnnotationView *av in views)
{
if ( [av.annotation isKindOfClass:[MKUserLocation class]])
{
[mapView selectAnnotation:av.annotation animated:NO];
//Setting animated to YES for the user location
//gives strange results so setting it to NO.
return;
}
}
}

Title and The subtitle can be added to the user location displayed by iOS using MKUserLocation. When the user clicks on the location, these titles and subtitles will be displayed in the bubble above the location. By using setSelected:animated:from MKAnnotatio nView selects a comment and can display thought bubbles of other comments. Unfortunately, MKUserLocation does not come from MKAnnotationView.

How to programmatically select the user location so that the comment is displayed on the user location without the user Click it first?

The documentation of MKAnnotationView explains its setSelected:animated:method (and the similar content of its selected properties):

You should not call this method directly.

Instead, use the MKMapView method selectAnnotation: animated:. If you call it in the didAddAnnotationViews delegate method, then You can ensure that the annotation view is ready to display annotations, otherwise calling selectAnnotation will not perform any operation.

For example:

-(void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
for (MKAnnotationView *av in views)
{
if ([av.annotation isKindOfClass:[MKUserLocation class]])
{
[mapView selectAnnotation:av.annotation animated:NO];
//Setting animated to YES for the user location
//gives strange results so setting it to NO.
return;
}
}
}

Leave a Comment

Your email address will not be published.