在Android上的JavaScript地理位置不起作用 [英] javascript geolocation on android doesn't work

查看:147
本文介绍了在Android上的JavaScript地理位置不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个网站,依靠拉动移动用户的地理定位数据。我通过做它的典型方式:

I am developing a website that relies on pulling the geolocation data of a mobile user. I am doing it the typical way via:

function initialize() {
   if(window.google && google.gears) {
        var geo = google.gears.factory.create('beta.geolocation');
        geo.getCurrentPosition(useLocation, errorOnLocate);
   }
   else if(navigator.geolocation) {
       navigator.geolocation.getCurrentPosition(useLocation, errorOnLocate);
   }
   else {
       alert("no geo found");
   }
}

function errorOnLocate(message)
{
    alert(message);
    zoomTo(-34.397, 150.644);
}

这总是失败,[对象PositionError]到errorOnLocate功能,即使我转地理位置法秩序。

This always fails with [object PositionError] in to the errorOnLocate function, even if I switch the geolocation method order.

这是上HTC Hero采用了Android 2.1使用任何浏览器是内置的。我的GPS是,我已指示浏览器允许网站查看我的位置。在谷歌地图本地应用程序的位置功能,来在手机上拿起我的位置就好了

This is on an HTC Hero with android 2.1 using whatever browser is built in. My gps is on and I have instructed the browser to allow websites to view my location. The "location" feature on the google map native application that comes on the phone picks up my location just fine

更进一步,如果我访问我的网站使用的是Firefox 3.5我的个人电脑上它会找到我的位置正确。(我相信它使用的IP和AP的数据点的组合)。无论哪种方式,它使用相同的JavaScript。

Further more if I visit my site using FireFox 3.5 on my personal computer it will find my location correctly.(I believe it uses a combination of ip and ap data points). Either way, it uses the same javascript.

编辑:这是在浏览器中的HTML / JS,不是本机应用程序。另外,确切的错误信息是最后的位置提供被禁用

This is html/js in a browser, not a native app. Also the exact error message is 'The last location provider was disabled'

推荐答案

这是正在从的WebView 或从Android浏览器应用程序访问?如果从的WebView ,则可能需要通过Java调用来实现地理定位。请参阅的WebView参考为。

Is this being accessed from a WebView or from the Android Browser app? If from a WebView, you may need to enable geolocation via a Java call. See the WebView reference for that.

否则,我不知道precisely有什么不对您的JS,但这里的HTML + JS,我知道的是Android浏览器的工作原理:

Otherwise, I'm not sure precisely what's wrong with your JS, but here's HTML+JS that I know works with the Android browser:

<!DOCTYPE html> 
<html> 
<head> 
  <script type="text/javascript"> 
function watchLocation(successCallback, errorCallback) {
  successCallback = successCallback || function(){};
  errorCallback = errorCallback || function(){};

  // Try HTML5-spec geolocation.
  var geolocation = navigator.geolocation;

  if (geolocation) {
    // We have a real geolocation service.
    try {
      function handleSuccess(position) {
        successCallback(position.coords);
      }

      geolocation.watchPosition(handleSuccess, errorCallback, {
        enableHighAccuracy: true,
        maximumAge: 5000 // 5 sec.
      });
    } catch (err) {
      errorCallback();
    }
  } else {
    errorCallback();
  }
}

function init() {
  watchLocation(function(coords) {
    document.getElementById('test').innerHTML = 'coords: ' + coords.latitude + ',' + coords.longitude;
  }, function() {
    document.getElementById('test').innerHTML = 'error';
  });
}
  </script> 
</head> 

<body onload="init()"> 
  <div id="test">Loading...</div> 
</body> 
</html>

这篇关于在Android上的JavaScript地理位置不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆