2016-12-02 88 views
0

我想将用户当前的经度和纬度合并到iframe网址中。我的代码如下。使用Javascript来获取经纬度

<script> 

var lat = position.coords.latitude; 
var lon = position.coords.longitude; 

document.write('<iframe src=http://weather.wdtinc.com/imap/imap5/?&INIT_LAT='+lat+'&INIT_LON='+lon+'> </iframe>'); 
</script> 

这只是给了我一个空白页。有谁知道我做错了什么?

+1

我不想念报价;不懂为什么人们宁愿问对这里等天知道多久答案比它只是谷歌.. HTTPS://developer.mozilla。 org/en-US/docs/Web/API/Geolocation/Using_geolocation –

+1

不要使用'document.write()'...永远。相反,将iFrame硬编码到页面的“主体”中,并给它一个“id”。然后通过DOM访问iFrame,然后将其注入。更重要的是,您的网页会尝试通过该iFrame访问其他网域的内容,并且很可能违反了“同源政策”(https://developer.mozilla.org/en-US/docs/Web/Security/ Same-origin_policy),可以在所有现代浏览器中使用。 –

+0

你使用什么浏览器?地理定位只能在安全网站(https://)上由Chrome浏览器进行。 – geocodezip

回答

2

您在SRC

navigator.geolocation.getCurrentPosition(function(position) { 
     document.write('<iframe src="http://weather.wdtinc.com/imap/imap5/?&INIT_LAT=' + 
     position.coords.latitude+ 
     '&INIT_LON='+ 
     position.coords.longitude+'" > </iframe>'); 
    });