How to check if there are any markers within a Google Maps viewport?

牧云@^-^@ 提交于 2019-12-05 01:10:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!