小编ahs*_*ali的帖子

在 iframe 中加载 HTML5 地理定位时访问用户的经纬度

我正在使用 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)

html javascript iframe geolocation

7
推荐指数
2
解决办法
7438
查看次数

标签 统计

geolocation ×1

html ×1

iframe ×1

javascript ×1