I\'m trying to make a website that ask for user\'s location then find the closest location (100m radius) from it\'s position using GeoLocation and display the result in HTML.
get current user's location using HTML5 geolocation and find nearest location within 100 meters.
include and use below google maps libs
Snippet
//calculates distance between two points in km's
function calcDistance(p1, p2) {
return (google.maps.geometry.spherical.computeDistanceBetween(p1, p2) / 1000).toFixed(2);
}
function getPosition(position) {
var userPosition = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
$.getJSON("places.json", function(data) {
for (var i = 0; i < data.length; i++) {
var p1 = new google.maps.LatLng(userPosition.lat, userPosition.lng);
var p2 = new google.maps.LatLng(data[i].lat, data[i].lng);
var distance = calcDistance(p1, p2) * 1000;
if ((distance * 1000) <= 100) {
html += '' + data[i].location + ' - ' + data[i].code + '
';
$('#nearbystops').append(html);
}
}
})
}
// get user's current latitude & longitude
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(getPosition);
} else {
alert("Geolocation is not supported by this browser.");
}
}
getLocation();