Testing my code i noticed that the geolocation accuracy does not correctly update. Note that geolocation longitude and latitude correctly works fine: when i call my function they are correctly updated.
Moreover, using the site from mobile, I noticed that if I leave my site, then open the Google Maps App and then back to my site, the accuracy of the geolocation is updated. It seems strange...
Here my code:
var currentShopLat, currentShopLong, currentAccuracy
export function getAndVerifyCurrentGeolocation () {
currentShopLat, currentShopLong, currentAccuracy = ""
console.log("Starting accuracy: " + currentAccuracy)
$w("#geolocationLoadingGif").expand()
$w("#geolocationStatusText").text = "Localizzazione in corso. Attendi alcuni istanti..."
$w("#geolocationStatusText").expand()
setTimeout( function() {
wixWindow.getCurrentGeolocation()
.then( (obj) => {
currentShopLat = obj.coords.latitude; console.log("currentShopLat: " + currentShopLat) // 32.0971036
currentShopLong = obj.coords.longitude; console.log("currentShopLong: " + currentShopLong) // 34.774391099999995
currentAccuracy = obj.coords.accuracy; console.log("accuracy: " + currentAccuracy) // 29
$w("#infoGeolocationText").text = "Lat: " + currentShopLat + " - " + "Lon: " + currentShopLong + " - " + "(Precisione: " + currentAccuracy + ")"
$w("#infoGeolocationText").expand()
$w("#geolocationLoadingGif").collapse()
if (currentAccuracy > 20) {
console.log("Bad accuracy")
getAndVerifyCurrentGeolocation()
}
if (currentAccuracy <= 20) {
console.log("GOOD accuracy")
}
var currentGeolocation = "http://www.google.com/maps/place/" + currentShopLat + "," + currentShopLong; //imposto il link sul tasto "vedi posizione sulla mappa"
$w("#goToLocationButton").link = currentGeolocation;
$w("#goToLocationButton").target = "_blank";
$w("#goToLocationButton").expand();
} )
.catch( (error) => {
let errorMsg = error;
});
$w("#geolocationStatusText").collapse()
$w("#geolocationErrorText").text = ""
}, 4000);
}
Any suggestions? Thanks in advance!
Thank you so much for your answer. Speaking of the timeout, that was old code that I no longer use now. The new code is this:
export async function checkGeolocation() { //function to check/update the user's geolocation in local sotrage let functionResults; let userLatitude = ""; let userLongitude = ""; let userGeolocationAccuracy = ""; await wixWindow.getCurrentGeolocation() .then(async(res) => { console.log('res: ', res); userLongitude = res.coords.longitude; userLatitude = res.coords.latitude; if (userLongitude !== '' && userLatitude !== '') { //no errors case [...] userGeolocationAccuracy = res.coords.accuracy; console.log('lat: ', userLatitude, 'lon: ', userLongitude, 'acc: ', userGeolocationAccuracy); functionResults = 'ok'; } else { //error case functionResults = 'error'; } }) .catch((error) => { [...] functionResults = 'error'; }); return functionResults; }
This new code seems to work fine with the "getCurrentGeolocation ()" function the first time I call it. But, if after a few seconds I call it again, the function returns the exact same results as the first call (even if the position has changed).
So, are you sure that there is no limitation in the Wix "getCurrentGeolocation ()" function? If so, it could actually be a browser issue. Have you ever encountered a similar issue before?
Thanks for your time.
This is really a browser/device issue and not a Corvid issue. If you are calling the function properly, there's not much Wix can do about it.
Why are you wrapping it in a timeout? It seems like you're handling the returned Promise correctly.
@Yisrael (Wix) sorry for the mention, but i'm still having this issue and can't find a solution.
Thanks in advance.