我有一个页面,它在iFrame中嵌入了谷歌静态地图的实例。当页面加载时,此iFrame的初始实例加载一个设置位置。页面上有一个地址列表,用户可以点击这些地址,并根据用户的选择,通过AJAX重新加载带有嵌入谷歌地图的iFrame。
我在加载新URL时遇到了一些问题。下面是我目前正在使用的PHP
$database->mysqlquery("SELECT street_address
FROM favorites
WHERE id = $row_id");
while($row = mysql_fetch_array($database->results)){
$address = $row[street_address];
}
$url = "http://maps.googleapis.com/maps/api/staticmap?center=$address&zoom
=18&size=640x640&sensor=false";
我从我的数据库中获取地址并将其分配给一个变量。然后将变量传递到静态映射URL中。
定义了url后,我调用一个iFrame
<iframe width = "425" height = "315" frameborder = "0" scrolling = "no"
marginheight = "0" marginwidth = "0" src= "<?php echo $url; ?>" id = "iframe"></iframe>
当我直接访问php页面或通过AJAX调用页面时,iFrame不会加载。当我使用Chrome或Firefox查看页面时,我会看到以下源代码
<iframe width="425" height="315" frameborder="0" scrolling="no"
marginheight="0" `marginwidth="0"
src="http://maps.googleapis.com/maps/api/staticmap?center=116 Ponce De Leon Ave
NE, Atlanta, GA&zoom=18&size=640x640&sensor=false" id="iframe"></iframe>
如果我从上面获取src字符串并直接通过浏览器加载链接,则映射将加载。
为什么我的iFrame不加载?
更新
添加rawurlencode()以编码iFrame变量后的新$address HTML
<iframe width="425" height="315" frameborder="0" scrolling="no" marginheight="0"
marginwidth="0"
src="http://maps.googleapis.com/maps/api/staticmap?
center=116%20Ponce%20De%20Leon%20Ave%20NE%2C%20Atlanta%2C%20GA&
zoom=18&size=640x640&sensor=false" id="iframe"></iframe>
发布于 2013-04-13 14:15:02
您必须对street_address进行编码,例如通过rawurlencode()
在URL中不允许使用空格(当您在浏览器中直接使用空格时,它会工作,因为大多数浏览器都会自动对URL进行编码)
https://stackoverflow.com/questions/15991526
复制相似问题