jav*_*ens 3 typescript angular
我被困在geolocation. 实际上它工作正常,但是当我使用If else. 当用户单击允许位置时它工作但当用户单击不允许时出现问题,它不会插入else. 此DEMO作为参考。
成分
ngOnInit() {
this.getUserLocation();
}
getUserLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(position => {
this.lat = position.coords.latitude;
this.lng = position.coords.longitude;
});
}else {
console.log("User not allow")
}
}
Run Code Online (Sandbox Code Playgroud)
@shayan ans 是获得您想要的结果的正确方法。我将稍微修改他的方法以满足您的期望。
尝试这样的事情
navigator.geolocation.getCurrentPosition(function(){
alert('Location accessed')
},function(){
alert('User not allowed')
},{timeout:10000})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7469 次 |
| 最近记录: |