I want to make a div appear based on distance. For this I'm getting the location of the user and the coordinates of the place the div is supposed to appear and I'm being successful making this happen. My problem now, is that there's gonna be different places where I want this to be available, so I need to find a way to loop and constantly be checking where the person is and see if its close to the distance of my coordinates.
-- Getting Location of person
if (navigator.geolocation){
navigator.geolocation.watchPosition(showPosition)
}
-- Function to work div appearing
function showPosition(position){
let locationLatitude = []
let locationsLongitude = []
//AJAX CALL TO GET THE POSITION OF THE PLACES AND PUSHING THEM TO THE ARRAYS
let success = function(res){
let locations = res.locations
for (var i=0; i<locations.length; i++){
locationLatitude.push(locations[i]['place_latitude'])
locationsLongitude.push(locations[i]['place_longitude'])
}
}
$.ajax({
type: 'GET',
url: '/api/locations',
crossDomain: true,
dataType: 'json',
async: false,
success : success,
});
// LOOP TO GET ALL COORDIANTES FROM PLACES (LAT,LONG)
var locationLatitudeLength = locationLatitude.length
var locationsLongitudeLength = locationsLongitude.length
let startPosLat
let startPosLong
for(var i=0; i<locationLatitudeLength; i++){
for(var j=0; j<locationsLongitudeLength; j++){
startPosLat = locationLatitude[i]
startPosLong = locationsLongitude[j]
userlocationLatitude = position.coords.latitude
userlocationLongitude = position.coords.longitude
//PASS VALUES OF COORDINATES TO THE FUNCTION
let distance = calculateDistance(startPosLat, startPosLong, userlocationLatitude, userlocationLongitude)
}
}
if(distance < .05){
$('.div-image').attr('src', 'pic2.jpg')
}else if(distance > .05){
$('.div-image').attr('src', 'pic.jpg')
}
//function to calculate the distance between two points of coordinates
function calculateDistance(lat1, lon1, lat2, lon2) {
var R = 6371; // km
var dLat = (lat2-lat1).toRad();
var dLon = (lon2-lon1).toRad();
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
return d;
}
Number.prototype.toRad = function() {
return this * Math.PI / 180;
}
}
the problem with this is that it doesn't recognize the coordinates coming from the loop.
Anyone has a recommendation on how to make the function run for each coordinate on the loop to check if I'm there or not.