title: Google Maps API - Get n closest markers from location
body: <p>I've created a Google Map using the Maps API and now I would like to find the closest <code>n</code> markers to a given location. I found <a href="http://jsfiddle.net/ReneKorss/6DCTp/" rel="nofollow noreferrer">this Fiddle</a> which shows how to find the single closest marker, how can I adapt it so that it finds the closest <code>n</code> markers rather than just the single closest?</p>
  
  <pre><code>function find_closest_marker(event) {
      var distances = [];
      var closest = -1;
      for (i = 0; i &lt; markers.length; i++) {
          var d = google.maps.geometry.spherical.computeDistanceBetween(markers[i].position, event.latLng);
          distances[i] = d;
          if (closest == -1 || d &lt; distances[closest]) {
              closest = i;
          }
      }
      console.log('Closest marker is: ' + markers[closest].getTitle());
  }
  </code></pre>