HTML5地理定位问题

HTML5 Geolocation Trouble

本文关键字:问题 定位 HTML5      更新时间:2023-09-26

我正在为HTML5地理位置编写一个Java脚本,但它似乎不起作用,当我试图在另一个范围中输出内容时,它说未定义。下面是代码。

<script>
var lat;
var lng;
function getLocation()
  {
  if (navigator.geolocation)
    {
    navigator.geolocation.getCurrentPosition(showPosition);
    }
  else{}
  }
function showPosition(position)
  {
     lat = position.coords.latitude;
     lng = position.coords.longitude;
     document.write(lat); // Doesnt output anything
  }

</script>
    <script type="text/javascript">
          document.write(lat);      // It says undefined
        $(document).ready(function()
        {
             //some scripts here
         }
</script>

只有当html页面托管在web服务器上而不是本地主机上时,才会调用成功回调函数。您是在本地测试还是在Web服务器上测试?