0

I'm trying to get current location in my mobile app using Jquery. I followed this tutorial and i copied the sample. I saved the file as .html file and try to open from my phone. The code unable to get the current Latitude and longitude.

Display current GPS coordinates

<!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>
4

2 回答 2

0

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

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

于 2013-06-26T04:36:05.843 回答
0

用这个。这对我有用......它也应该上传到服务器。不会在本地主机上工作..

$(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");
      }
   });
});
于 2013-09-23T10:19:29.340 回答