javascript - 查看用户地理标记不起作用

标签 javascript html geolocation

我是 Javascript 的初学者,我正在尝试使这个示例正常工作。

这是一个简单的想法。

给定用户的位置,如果纬度小于 50,它将发送一条简单消息。

发生的情况是 if 根本不起作用.. 为什么?我需要创建一个新函数还是将 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>

最佳答案

  1. 永远不要为 userLat JavaScript 变量赋值。
  2. 在运行地理位置代码之前测试 userLat

将您的测试放在成功函数中,并测试position.coords.latitude

关于javascript - 查看用户地理标记不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21583424/

相关文章:

android - Android 应用程序中的反向地理编码

javascript数组顺序值从大到小

javascript - 不调用构造函数,但调用渲染

html - 无论图像或屏幕的宽度或高度如何,居中对齐图像

html - 在标题属性中添加换行符

Android GPS电池使用情况

javascript - 受 jquery.selectBox 插件影响的表单选择框的更改值

javascript - 通过调用函数来 react HOC; "Functions are not valid as a React child"

html - 具有 flex-grow 的相同宽度容器不起作用

javascript - AngularJS |根据地理位置(纬度、经度)对 JSON 进行排序