Tuesday, April 1, 2014

How to Remove All the Map Annotations except Current User Location Annotation

Today, working on the project focused on Location and Map View Programming. I need to update the Map Annotation Display when user zoom in and out the map. This would totally make sense here the Annotation will not be resizable and too many of the Annotations displayed in a very small area on the Map.

It's like this April Fool's Day, Google Map on mobile device gave people the fun of being a Pokemon Master by finding all the Pokemon, which is displayed as an Annotation on the map. It could be seen only when certain zoom level you are in. So, to find all the Pokemon, you have to zoom to certain level, then search it manually like a tank.

So, the question here is how can we remove these MKAnnotations.

This task is very easy by using one line code:

[self.mapView removeAnnotations:self.mapView.annotations];

The mapView is the outlet connected with MKMapView in the storyboard.

But this line of code would remove all of the annotations on the map. While my app intentionally need to keep the current user location's annotation to constantly display the current location of the user. How can we do that?

We need to iterate through all the annotations, find out which one is not the Current User Location Annotation.

for (id<MKAnnotation> annotation in self.mapView.annotations) {
    if (![annotation isKindOfClass:[MKUserLocation class]]) {
        [self.mapView removeAnnotation:annotation];

Simple solution.

Good luck with your own project.

No comments :

Post a Comment