I'm trying to get the match from the following array comparing the coordinates from the browser vs the coordinates on the array, but I get an 'undefined' value when I call the const 'matchgeosucursal' even if I force the condition of the filter to match, the goal of this code is to get the URL of the matching scenario, I think I'm using the filter function wrong.
//Request permissions
$( document ).ready(function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
console.log ("Geolocation is not supported by this browser.");
}
//Filter and gets match url
function showPosition(position) {
console.log("Latitude: " + position.coords.latitude)
console.log("Longitude: " + position.coords.longitude);
var navlat = position.coords.latitude; //Browser lat
var navlon = position.coords.longitude; // Browser lon
// Array
var sucursales = [
{lat: 21.924089, lon: -102.293378, url: "https://shorturl.at/test1"},
{lat: 21.859964, lon: -102.298034, url: "https://shorturl.at/test2"},
{lat: 32.45569, lon: -116.836181, url: "https://shorturl.at/test3"},
{lat: 24.114868, lon: -110.340389, url: "https://shorturl.at/test4"},
{lat: 21.0335386, lon: -89.559187, url: "https://shorturl.at/test5"}];
//Match result
const matchgeosucursal = sucursales.filter( sucursales => sucursales.lat === navlat && sucursales.lon === navlon);
console.log('Match> ' + matchgeosucursal);
}
});