Javascript 查看用户地理标记不起作用

Javascript 查看用户地理标记不起作用,javascript,html,geolocation,Javascript,Html,Geolocation,我是Javascript的初学者,我正在努力使这个例子起作用 这是一个简单的想法 给定用户的位置,如果Lat小于50,它将发送一条简单消息 所发生的情况是,if根本不起作用。。为什么?我是否需要创建一个新函数或将if放入其中 <!DOCTYPE html> <html> <head> <script type="text/javascript"> function initGeolocation() {

我是Javascript的初学者,我正在努力使这个例子起作用

这是一个简单的想法

给定用户的位置,如果Lat小于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>

函数initGeolocation()
{
if(navigator.geolocation)
{
//使用成功和失败回调调用getCurrentPosition
navigator.geolocation.getCurrentPosition(成功,失败);
}
其他的
{
警报(“对不起,您的浏览器不支持地理定位服务。”);
}
}
功能成功(职位)
{
document.getElementById('userLong')。value=position.coords.longitude;
document.getElementById('userLat')。value=position.coords.latitude
}
函数失败()
{
//无法获取位置
}
用户经度:
用户纬度:
if(userLat<50){ 文件。写(“很棒”) } else{document.write(“不太好”)}
  • 您从未为
    userLat
    JavaScript变量赋值
  • 在运行地理位置代码之前,先测试
    userLat

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

    请阅读Meta上有关的信息。“这不管用”不是一个好标题。[此评论后标题已被编辑。]知道什么都不起作用。。。函数成功(position){document.getElementById('userLong')。value=position.coords.longide;document.getElementById('userLat')。value=position.coords.latitude;var userLat=document.getElementById('userLong')。value;if(userLat<50){document.write(“Great”)}else{document.write(“notgreat”)}