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

不打扰是莪最后的温柔 提交于 2019-12-03 16:50:18

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).

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)

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