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

Javascript 地理定位在智能手机上不起作用,javascript,geolocation,Javascript,Geolocation,我想在我的应用程序中实现地理定位 if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(getPosition, errorGettingPosition); } function getPosition(position) { var myLatitude = position.coords.latitude;

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

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);
           }
 }
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(getPosition,errorGettingPosition);
}
功能getPosition(位置){
var myLatitude=位置坐标纬度;
var Mylongitute=位置坐标经度;
var Radiusarth=6371;
我的长度=我的长度*(Math.PI/180);
Mylatude=Mylatude*(Math.PI/180);
var x0=我的长度*半径地球*数学系数(我的纬度);
var y0=Mylatude*半径地球;
对于(变量i=0;i
我已经写了这些代码行。在电脑的浏览器中,它可以完美地工作,但如果我在我的智能手机(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中,我无法识别任何关于经度和纬度的位置。

是的,我可以看到。使用
watchPosition
并尝试在document ready.app上调用此函数,该应用程序在android中运行html或js文件需要webview。就像浏览器一样。它将像浏览器一样加载文档。在计算机浏览器或移动浏览器中,它具有许多特性和功能,并且应用程序
webview
非常紧凑,只会加载一个文档。如果您使用的是简单的<代码> WebVIEW <代码>应用程序,请考虑使用像 PooGePAP之类的框架工作,或者至少考虑这个<代码>文档。函数ondevicerady(){//your code}
发现:$scope.$apply()帮助我获取实际数据。