javascript - Android 在 Ionic 4 中无法获取地理位置
我正在使用来自 @ionic-native/geolocation/ngx 的地理定位,在用户进入查看 leaflet 地图时显示其位置,我的代码在浏览器和 iOS 手机上都能正常工作(我运行 ionic serve --devapp)。
我在安卓手机上收到一个错误,其错误为 '[object GeolocationPositionError]',错误信息为 "只允许安全来源(参见:goo.gl/Y0ZkNV)。"。有人能帮我吗?
此外,应用程序会自动请求使用GPS的权限,我的应用程序中还没有任何请求权限的代码。
this.geolocation.getCurrentPosition().then((resp) => {
resp.coords.latitude;
resp.coords.longitude;
let marker = L.marker([resp.coords.latitude, resp.coords.longitude]).addTo(this.map)
.bindPopup('Your Location')
.openPopup();
this.map.setView([resp.coords.latitude, resp.coords.longitude], 15);
console.log(resp.coords.latitude+" "+ resp.coords.longitude);
}).catch((error) => {
console.log('Error getting location ', error);
});
我是否还需要做其他事情才能使代码在安卓上运行?
非常感谢 :)
答案#1
当使用 Cordova 或 Capacitor 将应用部署到 Huawei 设备上的 Ionic 4 时,设备会提供地理位置,但在 ionic serve --devapp 上无法使用。