views:

93

answers:

2

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  A: 

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)

paullb
Thanks for the response. For 3-5 markers it'd function good, but as the number increases to hundreds/thousands (that's my case), it wouldn't be optimal.
Michał Pękała
I would think that the marker cluster breaks down for 1000s of markers. I tried it and it was pretty slow.
paullb
I could have used to big words. For now, I have tested ti with c.a. 1000 markers and it works like a charm, so I guess it would scale well up to at least a couple thousands.
Michał Pękała
A: 

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).
Michał Pękała