【发布时间】:2012-01-30 16:41:52
【问题描述】:
我有两个文本框,希望用户填写地理位置数据(纬度和经度)。我想要一个按钮,所以当用户点击它时,文本框会立即被地理定位数据填充。
感谢 w3schools,我可以将两个段落标签中的文本替换为位置,但我希望按钮填充文本框。这就是我现在所拥有的。谁有答案?
<!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>
【问题讨论】:
标签: html textbox geolocation latitude-longitude