I've created a Google Map using the Maps API and now I would like to find the closest n
markers to a given location. I found this Fiddle which shows how to find the single closest marker, how can I adapt it so that it finds the closest n
markers rather than just the single closest?
function find_closest_marker(event) {
var distances = [];
var closest = -1;
for (i = 0; i < markers.length; i++) {
var d = google.maps.geometry.spherical.computeDistanceBetween(markers[i].position, event.latLng);
distances[i] = d;
if (closest == -1 || d < distances[closest]) {
closest = i;
}
}
console.log('Closest marker is: ' + markers[closest].getTitle());
}