问题
My iPad App is retrieving geographical objects from a database. In this database some objects have the same longitude / Latitude. As a consequence in the MkMapView I'm adding several MKPinAnnotation with the same Lat/Long. Unfortunately only one is displayed, I suppose it the last one I have added.
How can I have only one MKPinAnnotation object (for a given coordinate) and when user clicks on it I will display MKPinAnnotation for each objects sharing the same coordinates? Something similar to what is done by Google Maps / Google Earth, you click on the main Pin and then several Pin are display.
Any help will be appreciated.
Sébastien.
回答1:
Do you think that multiple pins are being displayed on top of each other when they are at the exact same coordinate? Or is it possible that Apple culls the additional pins at the same coordinate?
Either way, I would suggest changing the coordinates so that they can be seen to be clearly separate. What you could do is find all the pins that are extremely close to each other by using the distanceFromLocation: method of CLLocation. Once you have found that there are some pins that are a tiny distance (say < 0.01) from each other, then you could amend their coordinates before adding them to the map. You could make them form a small circle around the actual exact coordinate, with the angle around the circle determined by how many in that cluster (e.g. for 4 pins, place them at 0, 90, 180, 270 degrees). Or you could change the latitude only, by a very small amount, so that the pins appear to stack up on each other in a vertical line.
You could also do this separation when the user taps on an individual pin. i.e. for that pin, check if there are any other pins that are very close. If there are, remove this pin (or maybe remove all those pins), calculate new positions for all the pins in a circle or vertical line, then re-add the pins to the map (and open any callout view if required).
Once the pins have been separated in this manner, it will be easy for the user to tap on the individual pins.
I'm not exactly sure how Google Maps/Google Earth deal with this issue - could you describe it or provide a screenshot? Or mention some search terms that will cause it to happen. But hopefully the ideas above will give you something to try.
回答2:
You could simply combine them to one annotationView
and when the user touches it make some fancy animation inside that annotationView
to present the content.
I have built a useful control for clustering nearby pins (also for pins with the same coordinate) on MKMapView
since i had the same problems a couple of times. It is called OCMapView. It can combine the annotation for you, but you will have to implement the animation stuff on user touch on your own.
来源:https://stackoverflow.com/questions/10174044/how-to-display-several-mkpinannotation-with-same-latitude-longitude