[英]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.