0

I have the myLocation and a set of markers. I can successfully use fitBounds to display them all.

I want to zoom/fitbounds in to the myLocation plus the nearest 5 markers (to myLocation).

If i had the nearest 5, I could iterate thru them and bounds.extend(latlng); each one of them, and the myLocation. (I think)

How do I find the nearest 5?

Doug Cassidy
  • 1,796
  • 3
  • 17
  • 28
  • Yes, that's correct. What's your question, how to calculate the 5 nearest markers? – duncan Nov 16 '15 at 16:13
  • Possible duplicate of [Google Maps API - Closest marker function change to closest n markers](http://stackoverflow.com/questions/27905570/google-maps-api-closest-marker-function-change-to-closest-n-markers/) – geocodezip Nov 16 '15 at 16:15

1 Answers1

1

Here's what I did, on advice from Google Maps API - Closest marker function change to closest n markers

function sortByDist(a, b) {
    return (a.distance - b.distance)
}

function findClosestN(pt, numberOfResults) {
    var closest = [];
    for (var i = 0; i < markers.length; i++) {
        markers[i].distance = google.maps.geometry.spherical.computeDistanceBetween(pt, markers[i].getPosition());
        closest.push(markers[i]);
    }
    closest.sort(sortByDist);
    return closest.splice(0,numberOfResults);
}


closest = findClosestN(urHeremarker.position, 5);
for(i = 0; i < closest.length; i++) {
    bounds.extend(closest[i].position);
}
bounds.extend(urHeremarker.position);

map.fitBounds(bounds);
Community
  • 1
  • 1
Doug Cassidy
  • 1,796
  • 3
  • 17
  • 28