问题
What is the most effcient approach to check if there are any markers within a viewport?
A perfect solution wouldn't require checking all the markers one-by-one if it's contained by the viewport.
回答1:
The best solution that I came up with is to
- on the application launch, create an array with reference to markers - sorted by 1 coordinate, i.e. latitude
- get viewport's bounds with
GMap2.getBounds()
(as paullb suggested) - take lower latitude of viewport boundaries and look for it in the array (fastest to achieve with binary search)
- check if every following marker fits within the viewport (up to marker's latitude <= viewport's upper latitude).
回答2:
Use GMap2.getBounds() to find the bounding box. The use GLatLngBounds.containsLatLng() to check each marker to see if it is visible.
Alternatively you could try and use the same approach with the Marker Cluster if the value of each cluster is stored in an easily accessible way. (haven't looked myself)
来源:https://stackoverflow.com/questions/2949486/how-to-check-if-there-are-any-markers-within-a-google-maps-viewport