Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
574 views
in Technique[技术] by (71.8m points)

iphone - Remove MKMapView Annotations with a certain pinColor?

Is it possible to remove all annotations on a given MKMapView of a given pinColor? I'm trying to clear all user-entered annotations (pins) on my map before displaying new ones for a part of my app, but I didn't know how granular I can be in selectively removing annotations...

I'd like to just remove all annotations that have MKPinAnnotationColorGreen (green) pins, but I'd also be able to simply remove all pins entered by the user, if there's a way I could track those.

I know I could simply do:

[myMapView removeAnnotations:myMapView.annotations];

... then redraw all the annotations I want, but that seems like a waste of resources.

Any advice?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I am not able to test this right now, but have you tried:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"pinColor == %d",  MKPinAnnotationColorGreen];
[myMapView removeAnnotations:[myMapView.annotations filteredArrayUsingPredicate:predicate]];

As for the ones added by the user, you might need to keep track of those yourself. You could also create your own subclass of MKPinAnnotation. On that subclass, add the property

@property (nonatomic, BOOL) addedByUser;

. You could set addedByUser to true if they were added by the user and then filter out those using a similar approach above (eg. @"addedByUser == YES").


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...