When i click on map, which will be best way to find nearest marker or markers? is there some functions in api that will help me to do that?
it's google map api v3.
When i click on map, which will be best way to find nearest marker or markers? is there some functions in api that will help me to do that?
it's google map api v3.
The formula above didn't work for me, but I used this without any issue. Pass your current location to the function, and loop through an array of markers to find the closest:
Are you aware of Mysql Spatial extensions?
You could use something like MBRContains(g1,g2).
First you have to add the eventlistener
Then create a function that loops through the array of markers and uses the haversine formula to calculate the distance of each marker from the click.
This keeps track of the closest markers and alerts its title.
I have my markers as an array on my map object
You can use the computeDistanceBetween() method in the google.maps.geometry.spherical namespace.
Here is another function that works great for me, returns distance in kilometers:
I'd like to expand on Leor's suggestion for anyone confused on how to compute the nearest location and actually provide a working solution:
I'm using markers in a
markers
array e.g.var markers = [];
.Then let's have our position as something like
var location = new google.maps.LatLng(51.99, -0.74);
Then we simply reduce our markers against the location we have like so:
What pops out is your closest marker
LatLng
object.