Google map gets the 10 closest points of all markers on the map

I am trying to get the 10 closest markers from the center of a google map and sort them by center by distance in Javascript. So let's say that I have 100 markers in the Javascript array - I would like to show more information about the 10 closest places in the html unordered list. I found a similar example for version 2 api ( example in version 2 ), but nothing for version 3 of the google map api.

+6
source share
2 answers

Whatever happens, you need to calculate all the distances. You can do it yourself using simple equations or use Google geometry: http://code.google.com/intl/pl-PL/apis/maps/documentation/javascript/geometry.html and its function: computeDistanceBetween (). Then save the distance as a custom marker, for example, for example:

marker.distance = google.maps.geometry.spherical.computeDistanceBetween(marker.position, center.position); 

and sort it anyway. Hope this helps.

+16
source
  • Sort the array by proximity to the center point of the map. Use sort() .
  • Slice the first 10 with slice() .
  • Divide them on the map.
0
source

Source: https://habr.com/ru/post/902911/


All Articles