Google Maps Api v3 - find nearby markers

When I click on a map, what would be the best way to find the nearest marker or markers? are there any functions in api that will help me do this?

this is google map api v3.

+85
google-maps google-maps-api-3
Oct 30 '10 at 7:08
source share
8 answers

You must add an eventlistener first

google.maps.event.addListener(map, 'click', find_closest_marker); 

Then create a function that traverses the marker array and uses the haversine formula to calculate the distance of each marker from the click.

 function rad(x) {return x*Math.PI/180;} function find_closest_marker( event ) { var lat = event.latLng.lat(); var lng = event.latLng.lng(); var R = 6371; // radius of earth in km var distances = []; var closest = -1; for( i=0;i<map.markers.length; i++ ) { var mlat = map.markers[i].position.lat(); var mlng = map.markers[i].position.lng(); var dLat = rad(mlat - lat); var dLong = rad(mlng - lng); var a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(rad(lat)) * Math.cos(rad(lat)) * Math.sin(dLong/2) * Math.sin(dLong/2); var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); var d = R * c; distances[i] = d; if ( closest == -1 || d < distances[closest] ) { closest = i; } } alert(map.markers[closest].title); } 

It keeps track of nearby markers and warns of its name.

I have my markers as an array on my map object

+107
Oct 30 '10 at 21:17
source share

You can use the computeDistanceBetween () method in the google.maps.geometry.spherical namespace.

+75
Aug 07
source share

I would like to expand Leor's offer to anyone who is confused about how to calculate the closest location and actually provide a working solution:

I use markers in the markers array, for example. var markers = []; .

Then let our position be something like var location = new google.maps.LatLng(51.99, -0.74);

Then we simply reduce our markers from the location that we have like this:

 markers.reduce(function (prev, curr) { var cpos = google.maps.geometry.spherical.computeDistanceBetween(location.position, curr.position); var ppos = google.maps.geometry.spherical.computeDistanceBetween(location.position, prev.position); return cpos < ppos ? curr : prev; }).position 

What pops up your nearest LatLng marker.

+24
Jun 21 '16 at 13:06
source share

The formula above did not work for me, but I used it without any problems. Pass the current location of the function and skip the marker array to find the closest:

 function find_closest_marker( lat1, lon1 ) { var pi = Math.PI; var R = 6371; //equatorial radius var distances = []; var closest = -1; for( i=0;i<markers.length; i++ ) { var lat2 = markers[i].position.lat(); var lon2 = markers[i].position.lng(); var chLat = lat2-lat1; var chLon = lon2-lon1; var dLat = chLat*(pi/180); var dLon = chLon*(pi/180); var rLat1 = lat1*(pi/180); var rLat2 = lat2*(pi/180); var a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(rLat1) * Math.cos(rLat2); var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); var d = R * c; distances[i] = d; if ( closest == -1 || d < distances[closest] ) { closest = i; } } // (debug) The closest marker is: console.log(markers[closest]); } 
+9
Jul 22 '11 at 20:32
source share

Do you know the Mysql Spatial extensions ?

You can use something like MBRContains (g1, g2) .

+5
Nov 01 '10 at 7:57
source share

Here is another function that works great for me, returns the distance in kilometers:

  function distance(lat1, lng1, lat2, lng2) { var radlat1 = Math.PI * lat1 / 180; var radlat2 = Math.PI * lat2 / 180; var radlon1 = Math.PI * lng1 / 180; var radlon2 = Math.PI * lng2 / 180; var theta = lng1 - lng2; var radtheta = Math.PI * theta / 180; var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta); dist = Math.acos(dist); dist = dist * 180 / Math.PI; dist = dist * 60 * 1.1515; //Get in in kilometers dist = dist * 1.609344; return dist; } 
+4
Mar 31 '15 at 15:21
source share

Use the computeDistanceBetween () method of the Google map API to calculate the marker next to your location and the list of markers on the Google map.

Steps: -

  1. Create a marker on a Google map.
    function addMarker(location) { var marker = new google.maps.Marker({ title: 'User added marker', icon: { path: google.maps.SymbolPath.BACKWARD_CLOSED_ARROW, scale: 5 }, position: location, map: map }); }

  2. When you click the mouse, create an event to get the latitude, length of your location and pass it to find_closest_marker ().

     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; } } alert('Closest marker is: ' + markers[closest].getTitle()); } 

Follow this link and follow the instructions. You can bring the marker closer to your location.

+1
Jul 18 '19 at 11:13
source share

Follow the link https://github.com/vikesh-tonpe/Nearer-marker-to-given-location and follow the instructions. You can bring the marker closer to your location.

0
Jul 18 '19 at 8:54
source share



All Articles