【问题标题】:View user geotagging does not work查看用户地理标记不起作用
【发布时间】:2014-02-05 16:53:17
【问题描述】:

我是 Javascript 的初学者,我正试图让这个例子发挥作用。

这是一个简单的想法。

给定用户的位置,如果 Lat 为

发生的情况是 if 根本不起作用.. 为什么? didi 我需要创建一个新函数或将 if 放在其中一个。?

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">

        function initGeolocation()
         {
               if( navigator.geolocation )
                    {
                       // Call getCurrentPosition with success and failure callbacks
                       navigator.geolocation.getCurrentPosition( success, fail );

                    }
              else
                  {
                      alert("Sorry, your browser does not support geolocation services.");
                  }

        }

     function success(position)
         {

             document.getElementById('userLong').value = position.coords.longitude;
             document.getElementById('userLat').value = position.coords.latitude
         }

    function fail()
     {
           // Could not obtain location
     }




   </script>    
   </head>

   <body onLoad="initGeolocation();">
       <FORM NAME="rd" METHOD="POST" ACTION="index.html"> 
       User Longitude: <INPUT TYPE="text" NAME="Long" ID="userLong" VALUE=""><br>
       User Latitude : <INPUT TYPE="text" NAME="Lat"  ID="userLat"  VALUE=""><br>

       <script>
            if (userLat < 50){
           document.write(" Great")
        }
       else { document.write( "Not Great")}
       </script>

  </body>
 </html>

【问题讨论】:

  • 请阅读Meta上关于how to write a good title的信息。 “这行不通”不是一个好标题。 [自此评论以来,标题已被编辑。]

标签: javascript html geolocation


【解决方案1】:
  1. 您永远不会为 userLat JavaScript 变量赋值。
  2. 在运行地理位置代码之前测试 userLat

把你的测试放在成功函数里面,然后测试position.coords.latitude

【讨论】:

  • 知道什么都没有工作... function success(position) { document.getElementById('userLong').value = position.coords.longitude; document.getElementById('userLat').value = position.coords.latitude; var userLat = document.getElementById('userLong').value; if (userLat
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-16
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多