Get Latitude/Longitude Button to Fill Input Boxes

本秂侑毒 提交于 2019-12-13 03:36:34

问题


I have two text boxes that I want users to fill with geolocation data (latitude and longitude). I want to have a button, so when users click it, the text boxes are filled instantly with the geolocation data.

Thanks to w3schools, I can replace the text in two paragraph tags to the locations, but I'd like the button fill up the text boxes. Here's what I have now. Anyone have an answer?

<!DOCTYPE html>
<html>
<body>

<form>
Latitude: <input type="text" name="Latitude" /> <br />
Longitude: <input type="text" name="Longitude" />
</form>

<button onclick="getLocation()">Get Location</button><input type="submit" value="Submit" />

<p id="demo">Click the button to get your coordinates:</p>
<p id="demo2">Click the button to get your coordinates:</p>

<script>
var x=document.getElementById("demo");
var y=document.getElementById("demo2");
function getLocation()
  {
  if (navigator.geolocation)
    {
    navigator.geolocation.getCurrentPosition(showPosition);
    }
  else{x.innerHTML="Geolocation is not supported by this browser.";}
  }
function showPosition(position)
  {
  x.innerHTML=position.coords.latitude;  
  y.innerHTML=position.coords.longitude;    
  }
</script>

</body>
</html>

回答1:


You are not really saying what the problem is, but as far as I know innerHTML only works in FF and not IE. Try:

function showPosition(position) {
  x.value=position.coords.latitude;  
  y.value=position.coords.longitude;    
}


来源:https://stackoverflow.com/questions/9067389/get-latitude-longitude-button-to-fill-input-boxes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!