[英]How to pass the Javascript location(Longitude & Latitude) value in to the Textbox value?
<body>
<button onclick="getLocation()">Get coordinates</button>
<p id="demo"></p>
<script>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude;
}
</script>
<input type="text" value="long,lati"></input>
</body>
我需要将脚本中的纬度和经度放入文本框中,而无需按代码中的要求对按钮进行超频。
<input type="text" value = "" id='loc'>
window.onload = function(){
var x = document.getElementById('loc');
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.value = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.value = "Latitude: " + position.coords.latitude + ",Longitude: " + position.coords.longitude;
}
getLocation();
};
只需抓住输入元素并将其值attr设置为获得的lat&long。 这不是很好的解决方案,但是它应该可以完成您需要的工作(无需单击按钮)。
<body>
<button>Get coordinates</button>
<p id="demo"></p>
<script>
var x = document.getElementById("demo");
getLocation();
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude;
}
</script>
<input type="text" value="long,lati"></input>
</body>
要在文本框中获取经度和纬度,您需要获取文本框的ID并在其中传递lat long的值
这是一个Jsfiddle http://jsfiddle.net/2b4n03k1/
var x = document.getElementById("demo");
function showPositionInInput(position) {
var input = document.getElementById("location");
input.value = "Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude;
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude;
}
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPositionInInput);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
getLocation()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.