Strae
Strae

Reputation: 19445

How to get the nearest marker to a know point in the visible map area with Google Maps API v.3?

i've already read this question, but i have some regrets to apply in my situation becose i have many markers (atm ~5000, grown everyday).

In my application the user place his marker (lets call it marker A) clicking the map or via a geocode by address; I have to know if there is a marker in a specific radius of marker A.

As i said, i cant iterate throught all the markers, it will probably kill the user browser, and if possible i'll love to avoid ajax requests to search into my database.

Actually i insert the markers on the map via MarkerManager

Is there a way to search only between the markers in the map's visible area?

Upvotes: 2

Views: 3998

Answers (2)

GoG
GoG

Reputation: 310

you can use the method getBounds() which will retrieve LatLngBounds object. With this object, you can call the methods getNorthEast(); getSouthWest() methods which will retrieve the Northwest and Southeast bounds of your map respectively. Knowing these bounds, you can perform calculations in your application (pseudo code):

if (yourPointLatitude > southWestBoundLatitude && yourPointLatitude < northWestBoundLatitude && yourPointLongitude > northWestBoundLongitude && yourPointLongitude < southEastBoundLongitude)
{
    //take some action, the point is in the map bounds
}

Note: this case is only for specific geographic location -> it will only work for the northeast quarter of the world (for example Europe). If you want for different location, use other logic in the if statement and that's it...

Edit: look at these links for details:Map LatLngBounds

Upvotes: 0

Strae
Strae

Reputation: 19445

After many many research, i ended with the iteration solution (demo here).

To decrease the number of items to iterate with, i divide in base of the state (at the moment is enaught), maybe in the future i can add further divisions.

If it can be usefull to someone else, my 'solution' implies an array like:

places = {
    "it":[
        "Rome",
        "Venice"
    ],
    "fr":[
        "Paris",
        "Lyon"
    ],
    "es":[
        "Madrid",
        "Barcelona",
        "Girona"
    ]
}

And a google geocode with the user marker coords to get the state ('fr', 'it') where to look for places.

Upvotes: 1

Related Questions