使用HTML5地理位置API与Android 2.0+浏览器

4

我正在尝试在Android设备上使用HTML5 API的地理定位功能。我已经用Galaxy S LC(相当于Galaxy S1)进行了测试。Android版本为2.2.1。

我已经阅读了许多关于此问题的主题,但仍然没有找到解决方案。

我使用以下典型代码进行地理定位:

`

<script type="text/javascript">

document.write("Trying to locate you");

if(navigator.geolocation){
    document.write("Geolocation is supported");

    navigator.geolocation.getCurrentPosition(successCallback, errorCallback,
  {
    enableHighAccuracy : true,
    timeout : 10000, // 10s
    maximumAge : 0
  }
);


function successCallback(position){
    document.write("You have been located. Latitude :" + position.coords.latitude +", longitude : " + position.coords.longitude);       
};


function errorCallback(error){
    document.write("It didn't work");
    switch(error.code){
    case error.PERMISSION_DENIED:
        document.write("Permission denied");
        break;
    case error.POSITION_UNAVAILABLE:
        document.write("Position unavailable");
        break;
    case error.TIMEOUT:
        document.write("Timeout");
        break;
    case error.UNKNOW_ERROR:
        document.write("Unknown error");
        break;              
    }
}

}
else{
    document.write("La géolocalisation n'est pas supportée par le navigateur"); 
}

</script>

它在PC上使用Chrome和Opera非常好(但不适用于Firefox)。它也可以在移动设备上使用Opera。但是在Android浏览器上无法使用。其版本为2.2.1。 在此情况下发生了以下情况: 1. 出现以下消息: “尝试定位您” “支持地理定位” 2. 在超时时间到达后(这里是10秒),将显示一个空白页面。 函数sucessCallback或errorCallback未被调用。
当我不指定超时时间(timeout = infinity)时,如果启用了GPS,则可以正常工作。如果没有启用,则也会收到一个没有错误消息的空白页面。
我在Android浏览器上尝试了这个HTML5 Geolocation测试:http://html5demos.com/geo,它也无法工作。
我尝试了this code(第一个答案)。有时候它能够工作,但我们必须等待2分钟才能获得位置(如评论中所说)。
我还尝试通过Webview和所需的所有规格获取它,但效果完全相同。 这里说要解决Android浏览器上的此错误,我们只需要将enableHighAccuracy设置为true,但如果GPS未激活,则仍然不会调用任何函数(sucessCallback或errorCallback)。
如果有人有解决方案,那对我来说将是一个很大的帮助。
感谢您的帮助。
实际上,空白页面是由"document.write"函数引起的。我用innerHTML替换了它,现在它可以工作了。这意味着错误会被显示出来,并调用errorCallback。
现在的问题是,安卓浏览器只能使用GPS(高精度)来确定位置。如果GPS未启用或无法获取位置(例如在建筑物内),它将不返回位置。相反,Opera浏览器将使用中继网络(Cell-ID方法)来确定位置(在这种情况下精度小于150m)。
您知道如何强制安卓浏览器使用cell-ID来源吗?

请查看此帖子及其解决方案。https://dev59.com/5WXWa4cB1Zd3GeqPOpoA#11298468 - axs
2个回答

0

谷歌在今年1月份更改了Chrome浏览器中位置服务的行为。其中一个变化是,如果HTTP连接未加密,则Chrome不会返回当前位置坐标。几个月前我们也遇到了类似的问题,在我们的Web应用程序中,位置服务在所有客户端和设备(PC、手机、平板电脑等)上都能正常工作,除了Chrome浏览器。在该应用程序上安装SSL证书解决了所有地理位置问题。希望这可以帮助到您。


0

像这样尝试一下。应该可以了。

<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to get your coordinates:</p>
<button onclick="getLocation()">Try It</button>
<script>
var x=document.getElementById("demo");
function getLocation()
  {
  if (navigator.geolocation)
    {
        navigator.geolocation.getCurrentPosition(showPosition,showError,
          {
            enableHighAccuracy : true,
            timeout : 10000, // 10s
            //maximumAge : 0
          }
        );
    }
  else{x.innerHTML="Geolocation is not supported by this browser.";}
  }
function showPosition(position)
  {
  x.innerHTML="Latitude: " + position.coords.latitude + 
  "<br>Longitude: " + position.coords.longitude;    
  }
function showError(error)
  {
  switch(error.code) 
    {
    case error.PERMISSION_DENIED:
      x.innerHTML="User denied the request for Geolocation."
      break;
    case error.POSITION_UNAVAILABLE:
      x.innerHTML="Location information is unavailable."
      break;
    case error.TIMEOUT:
      x.innerHTML="The request to get user location timed out."
      break;
    case error.UNKNOWN_ERROR:
      x.innerHTML="An unknown error occurred."
      break;
    }
  }
</script>

</body>
</html>

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