将 watchPosition 与箭头功能一起使用
Using watchPosition with arrow function
我正在使用以下代码通过 watchPosition 获取用户位置。我还想为失败参数定义代码并将选项传递给 watchPosition
,但我不确定代码应该放在哪里。
if (navigator.geolocation) {
//Success callback defined, but where shouls fail & options go?
var positionTimer = navigator.geolocation.watchPosition((position) => {
var crd = position.coords;
console.log("Updated user loc...");
console.log(crd.latitude + " : " + crd.longitude);
this.myLatitude = crd.latitude;
this.myLongitude = crd.longitude;
this.updateUserLocation();
},
console.warn("Error getting user location"),
this.options);
}
else {
alert("Geolocation not supported in your browser!");
}
给你:
const options = {};
const id = navigator.geolocation.watchPosition((pos) => {
console.log(pos);
}, (err) => {
console.log(err);
}, options)
这是你想要的:
const positionTimer = navigator.geolocation.watchPosition(
(position) => {
// Your success code here
console.log(position);
},
(err) => {
// Your error code here
console.log(err);
},
{
// Options here
}
)
记住:
An arrow function expression has a shorter syntax than a function expression and does not bind its own this, arguments, super, or new.target. These function expressions are best suited for non-method functions, and they cannot be used as constructors.
你可以像使用普通的一样使用它们function
改变的东西就是上面提到的。
你也可以声明你的箭头函数:
const success = (position) => { /* Success code here */ }
const error = (err) => { /* Error code here */ }
const options = { /* Options... */ }
const positionTimer = navigator.geolocation.watchPosition(success, error, options);
我正在使用以下代码通过 watchPosition 获取用户位置。我还想为失败参数定义代码并将选项传递给 watchPosition
,但我不确定代码应该放在哪里。
if (navigator.geolocation) {
//Success callback defined, but where shouls fail & options go?
var positionTimer = navigator.geolocation.watchPosition((position) => {
var crd = position.coords;
console.log("Updated user loc...");
console.log(crd.latitude + " : " + crd.longitude);
this.myLatitude = crd.latitude;
this.myLongitude = crd.longitude;
this.updateUserLocation();
},
console.warn("Error getting user location"),
this.options);
}
else {
alert("Geolocation not supported in your browser!");
}
给你:
const options = {};
const id = navigator.geolocation.watchPosition((pos) => {
console.log(pos);
}, (err) => {
console.log(err);
}, options)
这是你想要的:
const positionTimer = navigator.geolocation.watchPosition(
(position) => {
// Your success code here
console.log(position);
},
(err) => {
// Your error code here
console.log(err);
},
{
// Options here
}
)
记住:
An arrow function expression has a shorter syntax than a function expression and does not bind its own this, arguments, super, or new.target. These function expressions are best suited for non-method functions, and they cannot be used as constructors.
你可以像使用普通的一样使用它们function
改变的东西就是上面提到的。
你也可以声明你的箭头函数:
const success = (position) => { /* Success code here */ }
const error = (err) => { /* Error code here */ }
const options = { /* Options... */ }
const positionTimer = navigator.geolocation.watchPosition(success, error, options);