位置不可用capacitor ionic.we无法访问Android设备中的位置,从而得到此错误消息。
capacitor-runtime.js:414 {message: "location unavailable"}message:
"location
unavailable"__proto__: Object
capacitor.logFromNative @ capacitor-runtime.js:414
fromNative @ capacitor-runtime.js:212
(anonymous) @ VM289:1
pages-add-add-module-es2015.js:186 Error getting location {message:
"location unavailable"}
答案 0 :(得分:1)
是Android设备还是模拟器?
如果我们谈论的是android设备,请检查要启用的GPS,因为某些手机无法启用此功能。
在模拟器上,该插件无法正常工作。
答案 1 :(得分:0)
对于模拟器,你必须先设置一个默认位置,
go to emulator settings -> Location -> Search for a place(set the marker) -> click 'Set Location'
然后在您的组件中,您可以执行以下操作:
import { Capacitor, Plugins } from "@capacitor/core";
const { Geolocation } = Plugins; // don't inject in ctor()
Geolocation.getCurrentPosition({
enableHighAccuracy: true,
}).then((resp) => {
this.lat = resp.coords.latitude;
this.lng = resp.coords.longitude;
}).catch((error) => {
console.log('Error getting location', error);
});
并检查 GPS 权限,您可以使用以下内容
isGpsPermissionEnabled(permissions: AndroidPermissions): Promise<boolean> {
return await new Promise((resolve, reject) => {
if (Capacitor.isNative) {
permissions.checkPermission(permissions.PERMISSION.ACCESS_FINE_LOCATION).then(
rs => resolve(rs.hasPermission),
err => {
console.log("something went wrong")
}
);
}
else { resolve(true); // for browsers
}
})
}