I would like to expand Leor's offer to anyone who is confused about how to calculate the closest location and actually provide a working solution:
I use markers in the markers array, for example. var markers = []; .
Then let our position be something like var location = new google.maps.LatLng(51.99, -0.74);
Then we simply reduce our markers from the location that we have like this:
markers.reduce(function (prev, curr) { var cpos = google.maps.geometry.spherical.computeDistanceBetween(location.position, curr.position); var ppos = google.maps.geometry.spherical.computeDistanceBetween(location.position, prev.position); return cpos < ppos ? curr : prev; }).position
What pops up your nearest LatLng marker.
Jonathan Jun 21 '16 at 13:06 2016-06-21 13:06
source share