ahs*_*ali 7 html javascript iframe geolocation
我正在使用 HTML5 Geolocation 来获取用户的纬度和经度。当页面直接打开时,它在所有浏览器上都能正常工作,但现在我必须将地理定位代码放在 iframe 中。放入 iframe 后,geolocation 不会提示输入用户坐标。
当页面在 iframe 中加载时,如何获取用户的经纬度?
假设这是代码:
<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to get your coordinates:</p>
<button onclick="getLocation()">Try It</button>
<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>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
向allow="geolocation"iframe 标签添加属性对我有用。
它现在正确地要求地理定位权限。
希望这对它有帮助,它是一个 javascript 文件,它应该可以工作
var btn = $('div.main a.btn'),
longitude = $('#longitude'),
latitude = $('#latitude');
function savePosition(position) {
var long = position.coords.longitude,
lat = position.coords.latitude
longitude.text(long);
latitude.text(lat);
}
function positionError() {
$('div.main').prepend('<p class="error"><strong>Sorry!</strong> There was an error getting your location.</p>');
}
btn.click(function(elem) {
elem.preventDefault();
if (navigator.geolocation) {
// Can use geolocation, proceed with getting the location
navigator.geolocation.getCurrentPosition(savePosition, positionError);
} else {
// Can't use geolocation, we should tell the user
$('div.main').prepend('<p class="error">Your browser doesn\'t support geolocation. Try upgrading to a newer browser like <a href="http://chrome.google.com/" target="_blank">Google Chrome</a></p>');
}
});
Run Code Online (Sandbox Code Playgroud)
在你的html中
<a href="#" class="btn">Get my location!</a>
<p>
Longitude: <span id="longitude"></span>
<br>
Latitude: <span id="latitude"></span>
</p>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7438 次 |
| 最近记录: |