简体   繁体   English

HTML5 GeoLocation不适用于Android

[英]HTML5 GeoLocation not working with Android

My device is a HTC One X, browser is Chrome. 我的设备是HTC One X,浏览器是Chrome。 I'm trying to get HTML5 GeoLocation to work in browser however I'm unable to, it works on iOS and desktop however nothing for my Android. 我正在尝试使HTML5 GeoLocation在浏览器中运行,但是无法运行,它可以在iOS和台式机上运行,​​但是对于我的Android没有任何帮助。 Below is my code: 下面是我的代码:

<!DOCTYPE html>
<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
    <meta content='width=device-width, initial-scale=1, maximum-scale=1' name='viewport'>
      <title>HTML5 Test</title>
      <script src='http://cloud.keepiteasy.net/libs/modernizr.custom.89661.js' type='text/javascript'></script>
      <script src='http://cloud.keepiteasy.net/libs/jquery.js' type='text/javascript'></script>
    </meta>
  </head>
  <body>
    <script type="text/javascript">
      $(function() {
        if (Modernizr.geolocation) {
          navigator.geolocation.getCurrentPosition(success, error);
        }

        function success(position) {
          var lat = position.coords.latitude;
          var lng = position.coords.longitude;

          alert(lat);
          alert(lng);
        }

        function error(err) {
          if(err.code == 1) { 
            alert("Error: Access is denied!"); 
          }else if( err.code == 2) { 
            alert("Error: Position is unavailable!"); 
          } 
        }
      });
    </script>
  </body>
</html>

UPDATE: I fixed the doctype UPDATE: I updated the error function UPDATE: On my HTC I am still getting nothing, not even an error. 更新:我修复了文档类型更新:我更新了错误函数更新:在我的HTC上,我什么也没有得到,甚至没有出现错误。 On my Nexus 7 (just tried it), it works fine... WTF, hardware issue? 在我的Nexus 7(刚刚尝试过)上,它可以正常工作... WTF,硬件问题? But other GPS based apps work... 但是其他基于GPS的应用程序也可以工作...

I got the same issue on my HTC One X. At least you can make sure, your error function gets called, by adding a timeout: 我在HTC One X上遇到了相同的问题。至少可以确保通过添加超时来调用您的错误函数:

navigator.geolocation.getCurrentPosition(success, error, {timeout:3000});

In this example, your error function gets called after 3 seconds. 在此示例中,您的错误函数在3秒钟后被调用。

似乎是某种硬件问题,因为没有网站可以在OneX上获得我的地理位置,但是我的Nexus 7上面的代码可以正常工作。

Just restart your phone, guys. 伙计们,只需重新启动手机即可。 Yeah it's kinda stupid but it's the solution. 是的,这有点愚蠢,但这是解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM