使用Ionic Cordova进行地理定位返回不正确的纬度/经度值

3

我在使用Ionic和Cordova框架中的Geolocation插件时遇到了问题。

我正在使用Visual Studio Community 2015,Cordova CLI版本为:4.3.0,并将org.apache.cordova.geolocation插件添加到VSC-2015中。

我的controller.js文件:

.controller('MapCtrl', function ($scope) {  
function onSuccess(position) {            
        console.log(position.timestamp);
        console.log(position.coords.latitude + " " + position.coords.longitude);
    }
    function onError(error) {
        alert('code: ' + error.code + '\n' +
              'message: ' + error.message + '\n');
    }
    navigator.geolocation.getCurrentPosition(onSuccess, onError);


 })

将谷歌地图添加到index.html中

<script src="https://maps.googleapis.com/maps/api/js?sensor=true&v=3.exp"></script> 

在我的Map.html文件中添加了一张地图。

<div id="map" data-tap-disabled="true" map> </div>

问题在于我总是得到一个固定的经纬度值,即纬度为43.465187,经度为-80.522372。

这不是我正确的地理位置信息,我需要我的当前地理位置信息以经纬度表示。

请帮助我确定故障。

此外,我正在使用浏览器上的Ripple-Nexus(Galaxy)。

非常感谢任何帮助……


你能否在移动设备上尝试一下,而不是使用模拟器? - Anil kumar
2个回答

1

https://github.com/apache/cordova-plugin-geolocation下载最新版本的GeoLocation插件。

然后将$cordovaGeolocation注入到控制器中。代码如下:

.controller('HomeCtrl', function($scope,$cordovaGeolocation) {
$scope.getLocation = function() {      
  var posOptions = {timeout: 10000, enableHighAccuracy: false};
    $cordovaGeolocation
      .getCurrentPosition(posOptions)
      .then(function (position) {
        //these are your your lat long values  
        var lat  = position.coords.latitude
        var long = position.coords.longitude                        
        }, function(err) {
        // error           
       });
      }
    })

注意:设备需要连接到互联网或WiFi,并且需要在设备上保持GPS定位开启,以获取正确的纬度/经度值。

我使用了这个库,但有时会得到错误的位置。 - vuhung3990

0

我不确定为什么您会得到固定的经纬度值。但是,要使用Ionic获取设备位置,以下方法对我有效:

ionic.Platform.ready($scope.getLocation);

$scope.getLocation = function () {

    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition($scope.showPosition, $scope.showError);
    }
    else {
        console.log('Geolocation is not supported by this browser.');
    }
}

$scope.showPosition = function (position) {
 console.log(position.timestamp);
 console.log(position.coords.latitude + ' ' + position.coords.longitude);
}

$scope.showError = function (error) {
    switch (error.code) {
        case error.PERMISSION_DENIED:
            console.log('User denied the request for Geolocation.');
            break;
        case error.POSITION_UNAVAILABLE:
            console.log('Location information is unavailable.');
            break;
        case error.TIMEOUT:
            console.log('The request to get user location timed out.');
            break;
        case error.UNKNOWN_ERROR:
            console.log('An unknown error occurred.');
            break;
    }
}

请注意调用:
ionic.Platform.ready($scope.getLocation);

这是至关重要的,因为只有在设备准备/初始化后,您才能确保获得准确的GPS定位。


尝试了上述技术,但出现以下错误:错误:[$injector:unpr] 未知提供者:$cordovaGeolocationProvider <- $cordovaGeolocation <- MapCtrlhttp://errors.angularjs.org/1.4.3/$injector/unpr?p0=%24cordovaGeolocationProvider%20%3C-%20%24cordovaGeolocation%20%3C-%20MapCtrl - Mufaddal
啊,那是因为我写的例子是针对 Cordova 的。等我度假回来后,我会更新我的答案以适用于 Ionic。 - Ben Smith
嗨@Mufaddal,抱歉回复晚了。我已经使用Ionic重写了我的答案,而不是使用Cordova。如果有帮助,请告诉我。 - Ben Smith
我找到了问题所在,虽然可能听起来很愚蠢,但问题是获取正确的纬度/经度值,设备需要连接到互联网或WiFi,并且需要在设备上保持GPS位置开启。 - Mufaddal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接