Created
November 14, 2014 19:05
-
-
Save vschmidt94/31f774774b04fb71d75d to your computer and use it in GitHub Desktop.
Using GeometryUtil.js to compute distances between markers on a Leaflet map
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // while we're at it, find the closest marker to user | |
| closest = L.GeometryUtil.closest(map3, points, myLL); | |
| for (i = 0; i < points.length; i++) { | |
| var delta = 1E-14; | |
| var latDiff = Math.abs(points[i].lat - closest.lat); | |
| var lngDiff = Math.abs(points[i].lng - closest.lng); | |
| if (((latDiff < delta) && (lngDiff < delta))) { | |
| closestIdx = i; | |
| closestDist = myLL.distanceTo(points[i]); | |
| break; // if 2 markers are identical-enough, only return 1st | |
| } | |
| } | |
| // output results of nearest marker | |
| x = document.getElementById('nearestMarker'); | |
| x.className = 'highlighted'; | |
| x.innerHTML = 'NEAREST MARKER<br>Name: ' + points[closestIdx].name + | |
| '<br>Latitude: ' + points[closestIdx].lat + | |
| '<br>Longitude: ' + points[closestIdx].lng + | |
| '<br>Distance: ' + (Math.round(10 * (closestDist / 1000)) / 10) + | |
| ' km / ' + (Math.round(10 * (closestDist / 1609.34)) / 10) + ' mi'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment