Question:
Need Help identifying code issue on chrome.
I am using wixWindowFrontend.getCurrentGeolocation() to display locations close to a user. works perfectly on Firefox / Safari ect showing user location and surrounding locations. just not on Chrome. my site is secure and uses SSL certificates.
Website URL: https://www.club-connect.uk/
This is my code:
import wixWindowFrontend from 'wix-window-frontend';
import wixData from 'wix-data';
wixWindowFrontend.getCurrentGeolocation()
.then((obj) => {
// @ts-ignore
let timestamp = obj.timestamp; // 1495027186984
let latitude = obj.coords.latitude; // 32.0971036
let longitude = obj.coords.longitude; // 34.774391099999995
// @ts-ignore
let altitude = obj.coords.altitude; // null
// @ts-ignore
let accuracy = obj.coords.accuracy; // 29
// @ts-ignore
let altAccuracy = obj.coords.altitudeAccuracy; // null
// @ts-ignore
let heading = obj.coords.heading; // null
// @ts-ignore
let speed = obj.coords.speed;
console.log("lat: ", latitude, "long: ", longitude)
$w("#googleMaps2").location = {
latitude: latitude,
longitude: longitude,
"description": "Your Location",
};
// Fetch addresses from dataset
wixData.query('BRS-Clubs')
.limit(1000)
.find()
.then((result) => {
const items = result.items;
//console.log('Fetched Items:', items);
// Calculate distance for each address
items.forEach((item) => {
const addressLat = parseFloat(item.latitude); // Assuming latitude is stored as a string
const addressLon = parseFloat(item.longitude); // Assuming longitude is stored as a string
if (isNaN(addressLat) || isNaN(addressLon)) {
console.error("Invalid coordinates for item: ", item.title);
item.distance = Infinity; // Assign a high value to invalid entries
} else {
//console.log(`Address: ${item.title}, Lat: ${addressLat}, Lon: ${addressLon}`);
// Calculate distance
const distance = getDistance(latitude, longitude, addressLat, addressLon);
item.distance = distance;
//console.log(`Distance for ${item.title}: ${distance}`);
}
});
// Sort items by distance
items.sort((a, b) => a.distance - b.distance);
//console.log('Sorted Items by Distance:', items);
const limitedItems = items.slice(0, 5);
//console.log('Limited Items:', limitedItems);
// Update repeater data
// @ts-ignore
$w('#repeater3').data = limitedItems;
console.log("map viewable")
// Update repeater display
// @ts-ignore
$w('#repeater3').onItemReady(($item, itemData) => {
// console.log('Item Data:', itemData);
$item('#text60').text = itemData.title;
// @ts-ignore
$w('#box141').expand();
});
$w('#text62').collapse();
$w('#googleMaps2').expand();
})
.catch((error) => {
console.error('Error querying dataset:', error);
});
})
.catch((error) => {
// @ts-ignore
let errorMsg = error;
});
// Haversine formula to calculate the distance between two points
function getDistance(lat1, lon1, lat2, lon2) {
const R = 6371; // Radius of the Earth in kilometers
const dLat = deg2rad(lat2 - lat1);
const dLon = deg2rad(lon2 - lon1);
const a =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
const distance = R * c; // Distance in kilometers
return distance;
}
function deg2rad(deg) {
return deg * (Math.PI / 180);
}
Any help to get this working on chrome will be greatly appreciated.