开发者

Uniqueness of MKAnnotation

开发者 https://www.devze.com 2023-03-17 19:16 出处:网络
I have a MKMapView that queries a server and displays an array of MKAnnotations when the center is changed but i\'m experiencing the issue of duplicate MKAnnotations since I read them and add them.

I have a MKMapView that queries a server and displays an array of MKAnnotations when the center is changed but i'm experiencing the issue of duplicate MKAnnotations since I read them and add them.

I'm thinking on looking at each Annotation with a for and if it's already there do nothing, but I think is not the best solution to solve this.

Any ideas?

Edit: I'开发者_如何转开发m currently removing all of them and adding them again, which produces a flick for the user, thing i don't want to happen


In your annotation you can have a property to store a unique identifier like a primary key. When you get a the refreshed items you can search for the existing ones and add if not present.

OR

You can have a local mutable array and store the primary keys. Search that array and add to the array and map if the keys is not present.

On either case if the user keeps panning you will end up adding many annotation to the mapview.

Make sure you use so that you can reuse the views

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation;


Basic Options

Depending on your situation, it might be easiest to simply remove all of the existing ones, and add all of your queried ones.

I'm thinking on looking at each Annotation with a for and if it's already there do nothing, but I think is not the best solution to solve this.

Are you having a particular problem where this isn't working well for you?


More Advanced Option

Perhaps another, more interesting option, would be to split your map into grids the way Google does (zoom and grid x,y) The queries from the server should be specifically for a grid space, or a set of grid spaces - this way you're not re-requesting duplicate information in the first place. It also would make it easier for you to determine what is and what isn't already loaded. You could pre-request surrounding grid spaces in the background, and only load new grid spaces when the user scrolls out of the "already loaded" area.

0

精彩评论

暂无评论...
验证码 换一张
取 消