[英]Ionic: geolocation not working on Android
情況:
我正在使用Ionic Framework構建混合應用程序。 我需要獲取用戶位置。
我找到了一個名為Geolocation的cordova插件
它可以在瀏覽器中完美運行,但是無法在手機(Android)或模擬器(Genymotion)中測試該應用程序。
我知道在SO上也有類似的問題,但是解決方案對我不起作用。
編碼:
var options = { timeout: 10000, enableHighAccuracy: true };
$cordovaGeolocation.getCurrentPosition(options).then(function(position)
{
$scope.userLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
$scope.map.markers = [{
id: "first",
location_data: {
latitude: 45.3694868,
longitude: -73.9803275
}
},{
id: "second",
icon: "img/blue_marker.png",
location_data: {
latitude: $scope.userLocation.H,
longitude: $scope.userLocation.L
}
}];
}, function(error){
alert('error geolocation');
});
AndroidManifest.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
嘗試:
正如在其他SO答案中所讀到的那樣,我已經嘗試了有關選項的其他幾種組合,但是沒有一個組合可以解決問題。
var options = {timeout: 15000, enableHighAccuracy: true, maximumAge: 5000};
var options = {timeout: 10000, enableHighAccuracy: false, maximumAge: 0};
var options = {timeout: 10000, enableHighAccuracy: true, maximumAge: 0};
控制台中的錯誤消息:
k: no valid models attribute found
TypeError: Cannot read property 'length' of undefined
at k.didQueueInitPromise
TypeError: Cannot read property 'gManager' of undefined
at Object.fn
TypeError: Cannot read property 'length' of undefined
at Object.fn
PositionError {message: "Timeout expired", code: 3, PERMISSION_DENIED: 1, POSITION_UNAVAILABLE: 2, TIMEOUT: 3}
問題:
為什么Geolocation在瀏覽器中可以正常運行,但在Android上卻不能正常運行? 確切原因是什么? 我如何使其正常工作?
謝謝!!
這取決於您的cordova版本。 將其“ npm update -g cordova”更新為Latest。 如果這不起作用,請嘗試使用此方法navigator.geolocation.getCurrentPosition。
嘗試以下操作: http : //ionicframework.com/docs/v2/native/geolocation/請不要編寫 import語句。
$ ionic插件添加cordova-plugin-geolocation
直接且無需導入。
Geolocation.getCurrentPosition().then((resp) => { //resp.coords.latitude //resp.coords.longitude }) let watch = Geolocation.watchPosition(); watch.subscribe((data) => { //data.coords.latitude //data.coords.longitude })
我正在使用cordova地理位置插件。
navigator.geolocation.getCurrentPosition(onSuccess,onError,options);
而且我也面臨着同樣的問題,它不適用於genymotion。 如下所示,將enableHighAccuracy設置為true對我有用。
var options = {
enableHighAccuracy: true,
maximumAge: 0
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.