在 navigator.geolocation 产生结果之前无法停止在 parasails 组件方法中的执行
Can't stop execution in parasails component method till navigator.geolocation yields the result
我需要在单击 ajax-button 时获取访问者的位置,然后才将表单数据发送到操作。
到目前为止,我可以根据 class 存在条件提交。
问题是我的代码中的 navigator.geolocation 条件没有得到解析,尽管向用户显示了同意弹出窗口并单击了确定。
它只是跳过这个并显示后备位置。
这是我更改的默认风帆 ajax-button 组件:
methods: {
click: async function () {
let btnDelayed = document.getElementById('btn-search');
if (btnDelayed && btnDelayed.classList.contains('delayed')) {
console.log('btn-search cannot emit click');
await this.loadGeoData();
} else {
console.log('btn-search can emit click ');
this.$emit('click');
}
},
loadGeoData: async function () {
let loc = await this.getLocation();
console.log('location');
console.dir(loc);
/*
let test = {
lat: 0.22,
lng: 0.55
};
*/
let btnSubmit = document.getElementById('btn-search');
btnSubmit.classList.remove('delayed');
let pos = JSON.stringify(loc);
this.$emit('pos', pos);
console.log('pos event value ' + pos);
this.click();
},
getLocation: async function () {
let loc = {
lat: 0,
lng: 0
};
if (navigator.geolocation) {
let options = {
enableHighAccuracy: true,
timeout: 10000,
maximumAge: 0
};
console.log('geolocation loaded');
navigator.geolocation.getCurrentPosition(function (position) {
console.log('getting position...');
console.dir(position);
loc.lat = position.coords.latitude;
loc.lng = position.coords.longitude;
}, (err) => {
console.warn(`ERROR(${err.code}): ${err.message}`);
}, options);
}
return loc;
}
}
这是控制台日志输出:
btn-search cannot emit click
geolocation loaded
location
{…}
lat: 0
lng: 0
<prototype>: Object { … }
pos event value {"lat":0,"lng":0}
btn-search can emit click
// getting the data in the instance
argins
{…}
__ob__: Object { value: {…}, dep: {…}, vmCount: 0 }
actionref: "{\"lat\":0,\"lng\":0}"
actiontype: "word"
search:
虽然执行了 avigator.geolocation.getCurrentPosition,但提交表单后:
getting position... ajax-button.component.js:108:13
Position
coords: Coordinates { latitude: 44.00000000000, longitude: 26.00000000000, accuracy: 931, … }
timestamp: 1548163982134
_
当然需要先执行
对于更多的读者,
经过几天的搜索,我找到了 post、Catch Geolocation Error - Async Await,解释了如何使用 async/await 进行地理定位。
剩下的就是修改ajax-button组件的点击事件了
我需要在单击 ajax-button 时获取访问者的位置,然后才将表单数据发送到操作。
到目前为止,我可以根据 class 存在条件提交。
问题是我的代码中的 navigator.geolocation 条件没有得到解析,尽管向用户显示了同意弹出窗口并单击了确定。 它只是跳过这个并显示后备位置。
这是我更改的默认风帆 ajax-button 组件:
methods: {
click: async function () {
let btnDelayed = document.getElementById('btn-search');
if (btnDelayed && btnDelayed.classList.contains('delayed')) {
console.log('btn-search cannot emit click');
await this.loadGeoData();
} else {
console.log('btn-search can emit click ');
this.$emit('click');
}
},
loadGeoData: async function () {
let loc = await this.getLocation();
console.log('location');
console.dir(loc);
/*
let test = {
lat: 0.22,
lng: 0.55
};
*/
let btnSubmit = document.getElementById('btn-search');
btnSubmit.classList.remove('delayed');
let pos = JSON.stringify(loc);
this.$emit('pos', pos);
console.log('pos event value ' + pos);
this.click();
},
getLocation: async function () {
let loc = {
lat: 0,
lng: 0
};
if (navigator.geolocation) {
let options = {
enableHighAccuracy: true,
timeout: 10000,
maximumAge: 0
};
console.log('geolocation loaded');
navigator.geolocation.getCurrentPosition(function (position) {
console.log('getting position...');
console.dir(position);
loc.lat = position.coords.latitude;
loc.lng = position.coords.longitude;
}, (err) => {
console.warn(`ERROR(${err.code}): ${err.message}`);
}, options);
}
return loc;
}
}
这是控制台日志输出:
btn-search cannot emit click
geolocation loaded
location
{…}
lat: 0
lng: 0
<prototype>: Object { … }
pos event value {"lat":0,"lng":0}
btn-search can emit click
// getting the data in the instance
argins
{…}
__ob__: Object { value: {…}, dep: {…}, vmCount: 0 }
actionref: "{\"lat\":0,\"lng\":0}"
actiontype: "word"
search:
虽然执行了 avigator.geolocation.getCurrentPosition,但提交表单后:
getting position... ajax-button.component.js:108:13
Position
coords: Coordinates { latitude: 44.00000000000, longitude: 26.00000000000, accuracy: 931, … }
timestamp: 1548163982134
_
当然需要先执行
对于更多的读者,
经过几天的搜索,我找到了 post、Catch Geolocation Error - Async Await,解释了如何使用 async/await 进行地理定位。
剩下的就是修改ajax-button组件的点击事件了