【问题标题】:Jquery mobile unable to get latitude and longitudejquery mobile无法获取经纬度
【发布时间】:2013-06-23 01:51:12
【问题描述】:

我正在尝试使用 Jquery 在我的移动应用程序中获取当前位置。我按照本教程进行操作,并复制了示例。我将文件保存为 .html 文件并尝试从手机打开。代码无法获取当前的经纬度。

显示当前 GPS 坐标

<!DOCTYPE html> 
<html> 
<head> 
  <meta name=viewport content="user-scalable=no,width=device-width" />
  <link rel=stylesheet href=jquery.mobile/jquery.mobile.css />
  <script src=jquery.js></script>
  <script src=jquery.mobile/jquery.mobile.js></script>
</head> 

<body> 

<div data-role=page id=home>
  <div data-role=header>
    <h1>Home</h1>
  </div>

  <div data-role=content>
    <span> Latitude : </span> <span id=lat></span> <br />
    <span> Longitude : </span> <span id=lng></span> <br />
  </div>
</div>

</body>
</html>

<script>

navigator.geolocation.getCurrentPosition (function (pos)
{
  var lat = pos.coords.latitude;
  var lng = pos.coords.longitude;
  $("#lat").text (lat);
  $("#lng").text (lng);
});

</script>

【问题讨论】:

  • 三星硬件以 navigator.geolocation.getCurrentPosition 的问题而闻名。例如,我有 Galaxy S2 和 S3,它们都无法让 getCurrentPosition 函数工作。

标签: jquery google-maps jquery-mobile gps


【解决方案1】:

您没有写出您要测试的手机类型。 但我在 iPhone 上试过一次,但它不起作用。 在 iOS 上,您需要为应用程序启用位置服务。位置服务在设置->常规下。

话虽如此,我没有通过你的脚本来查看是否有任何问题,但我在 jsfiddle 中尝试过,它在桌面上对我来说效果很好。

【讨论】:

  • 我正在 Galaxy nexus 上进行测试。安卓版本 4.2.1。
  • 您是否收到有关允许网站获得您的职位的问题的弹出窗口?另外,如果您的页面是公开的,请粘贴一个链接,我可以在我的某些设备上试一试。
【解决方案2】:

使用这个。这对我有用......它也应该上传到服务器。无法在本地主机上运行..

$(document).ready(function (e) {
  navigator.geolocation.getCurrentPosition(function (pos) {
      var lat = pos.coords.latitude;
      var lng = pos.coords.longitude;
      if (lat == null) {
        alert("GPS not activated");
      }
   });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-25
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2015-07-31
    相关资源
    最近更新 更多