开发者

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

开发者 https://www.devze.com 2023-02-20 00:49 出处:网络
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).

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 c开发者_Go百科all 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?


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号