无法访问cordova中的地理位置。我的位置不起作用

时间:2015-08-17 12:54:11

标签: javascript android geolocation ionic-framework cordova-plugins

我正在构建一个使用我们当前位置的应用。我正在使用cordova制作应用程序。我导入了我的谷歌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>

1 个答案:

答案 0 :(得分:0)

在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