Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get N Markers that are closest to the center in Google Maps

I'm trying to figure out how to sort the first 10 Markers on a Google map by distance from the center of the map in Javascript. So, let's say I have 100 Markers in an array, and I want to display more information about the first 10 closest markers in a HTML unordered list. How would I go about doing that? I found a similar example for Google Maps API version 2 here, but nothing for version 3.

like image 356
Tom Bauer Avatar asked Dec 02 '11 07:12

Tom Bauer


People also ask

How do I get a marker position on Google Maps?

You can add a simple marker to the map at a desired location by instantiating the marker class and specifying the position to be marked using latlng, as shown below.

What is the Google Maps marker called?

Jens Rasmussen, one of the inventors of Google Maps, a Danish computer programmer is credited with the creation of what is officially known as the map pin or pushpin .

How do I move a marker smoothly on Google Maps?

initialize() function creates a google Map with location Marker. transition() & moveMarker() are used to move location marker smoothly on Google Map.


1 Answers

Whatever happens You need to calculate all distances. You can do it yourself with simple equations or use Google's geometry library: http://code.google.com/intl/pl-PL/apis/maps/documentation/javascript/geometry.html and its function: computeDistanceBetween(). Then store distance in custom marker property like e.g:

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

and sort it anyway you want. Hope it helps.

like image 102
slawekwin Avatar answered Sep 19 '22 08:09

slawekwin