[英]cordova-plugin-geolocation not working on Android device
我已經在ionic / cordova應用程序中實現了cordova-plugin-geolocation插件。 當我在瀏覽器以及iOS設備上運行該應用程序時,它可以完美運行。 但是,當我嘗試在Android設備上運行該地圖時,不會顯示地圖,也不會獲取您當前的GPS坐標。 超時后,將引發以下錯誤:
[對象PositionError]
首先,我添加了插件(以及運行此cli的cordova-plugin-whitelist插件):
cordova plugin add cordova-plugin-geolocation
cordova plugin add cordova-plugin-whitelist
然后,將以下內容添加到我的index.html中:
<script src="//maps.googleapis.com/maps/api/js"></script>
<script src="lib/angular-google-maps/dist/angular-google-maps.min.js"></script>
然后,我將app.js修改為包含“ uiGmapgoogle-maps”:
var app = angular.module('myApp', ['ionic', 'ngCordova', 'uiGmapgoogle-maps']);
我的地圖HTML:
<!-- Google map -->
<ui-gmap-google-map center='map.center' zoom='map.zoom'>
<ui-gmap-marker coords="marker.coords" options="marker.options" events="marker.events" idkey="marker.id" closeClick="hideMarkerPopup()" onClicked="showMarkerPopup()"></ui-gmap-marker>
</ui-gmap-google-map>
最后是控制器中的邏輯:
ionic.Platform.ready(function() {
var posOptions = {
enableHighAccuracy: true,
timeout: 20000,
maximumAge: 0
};
// Display 'loading' dialog
$ionicLoading.show({
template: 'Loading...'
});
$cordovaGeolocation.getCurrentPosition(posOptions).then(function(position) {
// Apply new values to map
$scope.location = {};
$scope.location.lat = position.coords.latitude;
$scope.location.long = position.coords.longitude;
$scope.map = {
center: {
latitude: $scope.location.lat,
longitude: $scope.location.long
},
zoom: 16,
pan: 1
};
$scope.marker = {
id: 0,
coords: {
latitude: $scope.location.lat,
longitude: $scope.location.long
}
};
$scope.marker.options = {
draggable: false
};
// Dismiss 'loading' dialog
$ionicLoading.hide();
}, function(err) {
// Dismiss 'please wait' dialog
$ionicLoading.hide();
var alertPopup = $ionicPopup.alert({
title: 'GPS Error',
template: err
});
});
}); // ionic.Platform.ready END
但是,正如我所說。 它可以在瀏覽器和iOS設備上運行,但不能在Android設備上運行。有什么幫助嗎?
在所有情況下,我都不能讓geoLocation 100%良好,因此,如果cordovaGeoLocation給我一個錯誤,我將使用此外部服務:
/** i declare de external service */
$scope.getLocation = function (){
return $http.get("http://ip-api.com/json/" ,{
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
}
}).catch(function(error) {
console.log(error);
});
}
/** and then i use this */
$scope.getLocation().then(function (res){
var coords = {
Coordinates : {
latitude: res.data.lat
,longitude: res.data.lon
}
};
$scope.coords = coords.Coordinates;
}).catch(function (err){
console.log(err);
});
我正在回答自己的問題,因為我看到這是一個很多人都遇到的問題,並且關於如何解決它的信息並不多。 我遵循了本教程: http : //www.gajotres.net/using-cordova-geoloacation-api-with-google-maps-in-ionic-framework/我認為這不僅在於安裝白名單插件,還在於在index.html中正確的meta標簽(您可以在本教程中找到meta標簽)。 不能在Android設備上正常運行。
我遇到了完全相同的錯誤,解決方案是將以下內容添加到AndroidManifest.xml中(位於Platforms-> Android中)
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
顯然,該插件未將權限添加到清單中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.