簡體   English   中英

無法訪問Cordova中的地理位置。 我的位置不起作用

[英]Can't access geolocation in cordova. My location not works

我正在構建一個使用我們當前位置的應用程序。 我正在使用cordova制作應用程序。 我導入了Google api密鑰。 還使用了cordova-plugin-geolocation。

該地圖和我的位置未顯示。 有誰能夠幫助我?

我的index.hml

 $scope.checkLocation = function(){ if (window.cordova) { cordova.plugins.diagnostic.isLocationEnabled( function(e) { if (e){ successFunctionCall(); } else { alert("Location Not Turned ON"); cordova.plugins.diagnostic.switchToLocationSettings(); } }, function(e) { alert('Error ' + e); } ); } } $scope.getLocation = function(){ $scope.checkLocation(); $scope.supportsGeo = $window.navigator; $scope.position = null; $window.navigator.geolocation.getCurrentPosition(function(position) { showToast('Getting Location...'); $scope.$apply(function() { $scope.position = position; var link = "http://maps.google.com/maps?saddr="+$scope.position.coords.latitude+","+$scope.position.coords.longitude+"&daddr="+$scope.address; $window.open(encodeURI(link), '_blank', 'location=no'); }); }, function(error) { alert(error); },{enableHighAccuracy: true}); } function showToast(message) { if (window.plugins && window.plugins.toast) { window.plugins.toast.showShortCenter(message); } else $ionicLoading.show({ template: message, noBackdrop: true, duration: 2000 }); } 
 /* Empty. Add your own CSS if you like */ .scroll { height: 100%; } #map { width: 100%; height: 100%; } 
 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width"> <title></title> <link href="lib/ionic/css/ionic.css" rel="stylesheet"> <link href="css/style.css" rel="stylesheet"> <script src="lib/ionic/js/ionic.bundle.js"></script> <script src="cordova.js"></script> <script src="js/app.js"></script> <script src="js/controller.js"></script> <script src="http://maps.googleapis.com/maps/api/js?key=[my_api_key]&sensor=true"></script> </head> <body ng-app="starter"> <ion-pane> <ion-header-bar class="bar-stable"> <h1 class="title">Ionic Blank Starter</h1> </ion-header-bar> <ion-content ng-controller="controller"> <div id="map" data-tap-disabled="true"></div> <a ng-click="getLocation()" class="tab-item"> <i class="icon ion-navigate"></i>Navigate </a> </ion-content> </ion-pane> </body> </html> 

在phonegap中,我正在使用以下方法獲取當前位置:

// onSuccess Callback
// This method accepts a Position object, which contains the
// current GPS coordinates
//
var onSuccess = function(position) {
    alert('Latitude: '          + position.coords.latitude          + '\n' +
          'Longitude: '         + position.coords.longitude         + '\n' +
          'Altitude: '          + position.coords.altitude          + '\n' +
          'Accuracy: '          + position.coords.accuracy          + '\n' +
          'Altitude Accuracy: ' + position.coords.altitudeAccuracy  + '\n' +
          'Heading: '           + position.coords.heading           + '\n' +
          'Speed: '             + position.coords.speed             + '\n' +
          'Timestamp: '         + position.timestamp                + '\n');
};

// onError Callback receives a PositionError object
//
function onError(error) {
    alert('code: '    + error.code    + '\n' +
          'message: ' + error.message + '\n');
}

navigator.geolocation.getCurrentPosition(onSuccess, onError);

在清單文件中添加以下權限:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

將以下功能添加到config.xml文件:(在app / res / xml / config.xml中)

<feature name="Geolocation">
    <param name="android-package" value="org.apache.cordova.geolocation.GeoBroker" />
</feature>

請按照以下文檔進行phonegap地理位置定位: http ://docs.phonegap.com/en/edge/cordova_geolocation_geolocation.md.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM