Tutorial :google maps : How to find if any markers inside map



Question:

I have divided the my google map display in to numbers of parts, Now I want of find it out if any markers are positioned inside a/any particulate cell.

Any Help ?

Farther Explained :

I have got the map bounds by

map.getBounds();  

method and then farther divide it into numbers of sub-bounds.

also I have putted markers as

map.addOverlay(markerObject);  

Now , I want find if of the cells (which I got by dividing the map by bounds) is containing any markers or not .

I have divide the entire map bounds into numbers of sub bounds


Solution:1

So keep all markers in array. Each marker has a method called get_position( ). After you have finished division of map bound into small sub bounds, you just need to iterate over the sub bounds and check whenever the marker within it.
PS. Also take a look on it, in some cases could be useful.


Suppose you on sub bound cell:

var sub_bounds = new Array();  // here you've pushed into an array the sub bounds  for ( var i = 0; i<sub_bounds.length; ++i)  {      for ( var j = 0; j < markers.length; ++j)      {         var lat = markers[j].get_position( ).lat;         var lng = markers[j].get_position( ).lng;         if ( sub_bounds[i].sw.lat<lat && lat<sub_bounds[i].ne.lat &&              sub_bounds[i].sw.lng<lng && lng<sub_bounds[i].ne.lng)         // marker within cell, do whatever you need to do      }  }  


Solution:2

Here is an alternative to the above solution without re-iteration:

First - how big are your sub_bounds? Say 10 latitude and longitude degrees each.

Second - The position of the marker is (floor(marker.lat / 10), floor(marker.lng / 10))

Third - Each marker is added to the map and dropped in a bucket for that subdomain.

so (40, -78) would lie in bucket (4,7) i.e. bucket["4~7"]

Correction: would lie in bucket (4,-7) i.e. bucket["4~-7"]

which would contain all markers between 40 and 50 lat and -70 and -80 lng.

You can use GLatLngBounds as the object that holds all these markers in each bucket, which would give you a good set of methods to use, such as calculating center of the bucket depending on the markers currently in it.


Solution:3

Probably the best solution is given here: how to find out whether a point is inside a polygone:

How to detect that a point is inside a Polygon using Google Maps?


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »