【问题标题】:Javascript Form Input - GPS - GeoLocationJavascript 表单输入 - GPS - 地理位置
【发布时间】:2012-04-21 23:05:26
【问题描述】:

我想从用户的设备中获取当前的 GPS 位置,并且基本上将 Lat 和 Long 放入表单字段中。然后我可以在插入位置后做任何事情。

下面的代码适用于弹出警报位置,这是我开始使用的。我希望能够单击一个按钮并将其填充到表单字段中。

<html> 
<head> 


<script type="text/javascript"> 
function getLocationConstant()
{
  if(navigator.geolocation)
  {
    navigator.geolocation.getCurrentPosition(onGeoSuccess,onGeoFormLat,on GeoFormLong,onGeoError); 
  } else {
    alert("Your browser or device doesn't support Geolocation");
  }
}

// If we have a successful location update
function onGeoSuccess(event)
{
  alert(event.coords.latitude + ', ' + event.coords.longitude);
}

function onGeoFormLat(event)
{
  var myVal;
  myVal = document.getElementById(event.coords.latitude).value;
}

function onGeoFormLong(event)
{
  var myVal;
  myVal = document.getElementById(event.coords.longitude).value;
}

// If something has gone wrong with the geolocation request
function onGeoError(event)
{
  alert("Error code " + event.code + ". " + event.message);
}

// Called when we want to stop getting location updates
function stopGetLocation(event)
{
  navigator.geolocation.clearWatch(watchID); 
}

</script>


</head>
<body>
  <br><br>
  Latitude: <input type="text" id="Latitude" name="onGeoFormLat" value="">
  <br><br>
  Longitude: <input type="text" id="Longitude" name="onGeoFormLong" value="">
  <br>

  <br><br><br>
  <input type="button" value="Get Location" onclick="getLocationConstant()" />
  <br><br>

</body>
</html>

【问题讨论】:

    标签: javascript forms geolocation


    【解决方案1】:

    我的朋友帮了我... 这适用于 gr8

     <script type="text/javascript"> 
     function getLocationConstant()
    {
        if(navigator.geolocation)
        {
            navigator.geolocation.getCurrentPosition(onGeoSuccess,onGeoError);  
        } else {
            alert("Your browser or device doesn't support Geolocation");
        }
    }
    
    // If we have a successful location update
    function onGeoSuccess(event)
    {
        document.getElementById("Latitude").value =  event.coords.latitude; 
        document.getElementById("Longitude").value = event.coords.longitude;
    
    }
    
    // If something has gone wrong with the geolocation request
    function onGeoError(event)
    {
        alert("Error code " + event.code + ". " + event.message);
    }
     </script>
    
     <br><br>
     <cfform action="gps2.cfm" method="post">
     Latitude: <input type="text" id="Latitude" name="Latitude" value="">
     <br><br>
     Longitude: <input type="text" id="Longitude" name="Longitude" value="">
     <br><br>
     <input type="button" value="Get Location" onclick="getLocationConstant()"/>
     <br><br>
     <input type="submit" value="Add GPS Location" class=small>
     </cfform>
    

    【讨论】:

    • 适用于 Android 平板电脑 (Galaxy) - 三星 Galaxy II、iPhone、iPad、iPhone... iPhone/iPad 需要开启定位服务
    【解决方案2】:

    @Merle_The_Pearl 提供的代码也适用于 OSX 上的 Safari 和 Firefox,但值得注意的例外是,如果用户决定不提供他们的位置,Firefox 不会调用“onGeoError”。这似乎是 Mozilla 人忽略 W3C 规范 https://bugzilla.mozilla.org/show_bug.cgi?id=675533 的一个不幸的政策决定

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-19
      • 2018-02-01
      • 2011-03-30
      • 1970-01-01
      • 1970-01-01
      • 2017-07-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多