【发布时间】:2019-05-31 02:08:59
【问题描述】:
我正在使用 HTML5 地理位置,并希望在输入字段中打印返回的用户位置。
这可能吗?如果有怎么办?
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;
}
<input type="text">
<button onclick="getLocation()">Try It</button>
<p id="demo"></p>
【问题讨论】:
-
您没有任何 ID 为
demo的 HTML 元素?? -
对不起,我刚刚编辑了代码。
-
您的代码对我有用。在 chrome 上,它会在显示我的地理坐标之前弹出并请求许可。你授权了吗? (chrome chrome://settings/content/location?search=permission 中的权限)
-
当我点击允许时,它不显示纬度或经度。它一直有效,直到弹出许可。
标签: javascript jquery html css