tags:

views:

1903

answers:

2

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

A: 

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
    }
}
Artem Barger
Almost there , But confuse as If suppose I am on a sub-bounds cell then , How can I find that , this cell contains any markers or not?
openidsujoy
does it do the same thing , as sub_bounds[x].containsLatLng(markers[i].getLatLng()) ??
openidsujoy
yeap. thank for pointing, I have had to look at API reference better.
Artem Barger
OK thanks,But I was trying to find it from another way , Just have a method for 'sub_bounds' object [ like 'isEmpty();' which returns the area covered by a bound.]which will return if that bound contains any markers or not , not by looping throw the markers. .... any Idea .
openidsujoy
you know deeply in some dark place someone still have to iterate over the markers to see whenever they within your bounds.
Artem Barger
A: 

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.

Nael El Shawwa