I am currently working on a script that compares the coordinates from the browser with coordinates in an array. However, I am encountering an issue where the const 'matchgeosucursal' returns as 'undefined' even when I force the filter condition to match. The main objective of this code is to retrieve the URL associated with the matching scenario, and I believe there may be an error in how I'm using the filter function.
//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);
}
});