Hello Everyone. I am using wixWindow.getCurrentGeolocation to get the current location of my device. However, this sometimes gives undefined values as results. Can someone please help me ?
Hi,
The results are only available after the fulfillment of a Promise. Make sure that you access the results in the .then() routine or in the results returned after await . You can see an example of how this call is invoked in the getCurrentGeolocation API .
You can see how an app uses getCurrentGeolocation() in the forum post How to Use Google Maps Services in Wix Code .
Good luck,
Yisrael
Hi. Thanks for the reply. I understand that data will be available only after promise is complete. I have written the following code. Please verify
import wixLocation from ‘wix-location’;
import wixData from ‘wix-data’;
import wixWindow from ‘wix-window’;
$w.onReady(function () {
getDetails();
});
async function getDetails() {
$w("#repeater1").hide();
let latitude;
let longitude;
wixWindow.getCurrentGeolocation()
.then(async(obj) => {
latitude = obj.coords.latitude; // 32.0971036
longitude = obj.coords.longitude; // 34.774391099999995
})
.catch((error) => {
let errorMsg = error;
});
await wixData.query("Service")
.find()
.then((results) => {
var list = [];
let filteredResults = results.items;
console.log("list is " + filteredResults);
for (var i = 0; i < filteredResults.length; i++) {
list[i] = filteredResults[i].title;
}
console.log(filteredResults);
var removed = [];
var duplicates = list.reduce(function (acc, el, i, arr) {
if (arr.indexOf(el) !== i && acc.indexOf(el.trim()) < 0) acc.push(el);
return acc;
}, []);
console.log("Duplicates are" + duplicates); // = 1,3 (actual array == [1, 3])
for (var i = 0; i < filteredResults.length; i++) {
if (duplicates.includes(filteredResults[i].title)) {
if (!removed.includes(filteredResults[i].title)) {
removed.push(filteredResults[i].title);
filteredResults.splice(i, 1);
}
}
}
$w("#repeater1").data = filteredResults;
console.log(filteredResults);
$w("#repeater1").onItemReady(($w, itemData, index) => {
console.log(itemData);
$w("#text15").text = itemData.title;
$w("#repeater1").show();
$w("#container1").onClick((event, $w) => {
const data = $w("#repeater1").data;
let service = data.filter(item => item._id === event.context.itemId);
console.log("Service is " + service[0].title);
var loc = service[0].title;
loc = loc.replace(" ", "-");
console.log(loc);
if (latitude == undefined || longitude == undefined) {
latitude = 41.263906;
longitude = -95.860830;
}
wixLocation.to("/getorgs/" + loc + "/0/0/" + latitude + "/" + "/" + longitude);
//wixLocation.to("/getorgs/" + loc+"/0/0/");
//wixLocation.to("/getorgs/"+loc+"/1/10");
});
});
});