地理定位在智能手机上不起作用

Geolocation does not work on smartphone

本文关键字:不起作用 智能手机 定位      更新时间:2023-09-26

我想在我的应用程序中实现地理定位。

if (navigator.geolocation) {               
           navigator.geolocation.getCurrentPosition(getPosition, errorGettingPosition);
}
function getPosition(position) {
           var myLatitude = position.coords.latitude;
           var myLongitude = position.coords.longitude;
           var radiusEarth = 6371;
           myLongitude = myLongitude * (Math.PI / 180);
           myLatitude = myLatitude * (Math.PI / 180);
           var x0 = myLongitude * radiusEarth * Math.cos(myLatitude);
           var y0 = myLatitude * radiusEarth;
           for (var i = 0; i < list.length; i++) {
              var vendorLongitude = list[i].Longitude = list[i].Longitude * (Math.PI / 180);
              var vendorLatitude = list[i].Latitude = list[i].Latitude * (Math.PI / 180);
              var x1 = vendorLongitude * radiusEarth * Math.cos(vendorLatitude);
              var y1 = vendorLatitude * radiusEarth;
              var dx = x0 - x1;
              var dy = y0 - y1;
              var d = Math.sqrt((dx * dx) + (dy * dy));
              if (d < 1) {
                 list[i].Distance = Math.round(d * 1000) + " m";
              } else {
                 list[i].Distance = Math.round(d * 10) / 10 + " km";
              }
           }
           //add vendors to scope
           $scope.vendors = list;
}
function errorGettingPosition(err) {
           if (err.code == 1) {
              alert("User denied geolocation.");
           }
           else if (err.code == 2) {
              alert("Position unavailable.");
           }
           else if (err.code == 3) {
              alert("Timeout expired.");
           }
           else {
              alert("ERROR:" + err.message);
           }
 }

这些代码行是我写的。在电脑上的浏览器中,它会完美地工作,但如果我在智能手机(Android,5.1.1版)上安装这个应用程序,它就不工作了,我不知道为什么。它也不会进入智能手机上的错误功能。

你知道该怎么办吗?

这个可以进入错误函数

function initiate_watchlocation() {
    if (watchProcess == null) {
        watchProcess = navigator.geolocation.watchPosition(onSuccess, onError);
    }
}
var onSuccess = function(position) {
    var myLatitude = position.coords.latitude;
    var myLongitude = position.coords.longitude;
    };
    function onError(error) {
            alert_box('code: '    + error.code    + ''n' +
              'message: ' + error.message + ''n');
    }

考虑在document.ready.中运行它

它非常特别。当应用程序加载网站时,它会从getCurrentPosition()函数开始。然后我可以走过去,什么都没发生。但几秒钟后(大约5秒钟),它将通过成功回调。我将结果保存在$scope中,因为我使用AngularJS。但在GUI中,我无法识别任何关于经度和纬度的位置。