插入php代码后,地图页面变为空白

时间:2015-04-11 21:51:25

标签: php google-maps-api-3 maps infowindow

将php代码插入infowindow内容后,我的gmap页面变为空白。

google.maps.event.addListener(noi, 'mouseover', function() {
  var infowindow = new google.maps.InfoWindow({
    content: '<?php 
        if ($count==0){
        echo "No Open Tickets";
        }
    else{
        echo "<table>";
        foreach ($NOIcompliancearray as $SLA_Compliance=>$count) {
            $Npath = $Nimages[$SLA_Compliance];
            echo "<tr>";
            echo "<td><a href='city.php?city=Noida&compliance=".$SLA_Compliance."'><img src='IndiaImages/".$Npath."' title='".$SLA_Compliance."' ></td>";
            echo "<td>".$count."</td>";
            echo "</tr>";
            }
        echo "</table>";        
    }
  ?>'
    size: new google.maps.Size(100,100),
  });
google.maps.event.addListener(noi, 'mouseover', function() {
infowindow.open(map,noi);
setTimeout(function() { infowindow.close(map, noi) }, 5000);
});

如果我用一些静态内容替换php代码,它可以正常工作。此外,当我尝试打开网页源代码时,它给了我想要在信息窗口中看到的结果。我不确定我在哪里弄错了。

从网页源代码输出: 内容:

&#13;
&#13;
'<table><tr><td><a href='city.php?city=Noida&compliance=A'><img src='IndiaImages/Circle_Red.gif' title='A' ></td><td>3</td></tr><tr><td><a href='city.php?city=Noida&compliance=C'><img src='IndiaImages/Circle_Yellow.gif' title='C' ></td><td>10</td></tr></table>Noida'
&#13;
&#13;
&#13;

请帮助我理解错误并缓解问题。

1 个答案:

答案 0 :(得分:0)

查看功能

google.maps.event.addListener(noi, 'mouseover', function() {
  var infowindow = new google.maps.InfoWindow({
    content: '...'
    size: new google.maps.Size(100,100),
  });

用php得到

google.maps.event.addListener(noi, 'mouseover', function() {
  var infowindow = new google.maps.InfoWindow({
    content: '<table>
            <tr>
            <td><a href='
   // 'city.php?city=......' is after content : string ending sign `'` will be ignored by function !
    size: new google.maps.Size(100,100),
  });

'生成的第一个php将被解释为字符串的结尾 其余的将在html源代码中。你可以看到它,但被函数忽略了。

所以不要在这种代码中使用'

echo "<td><a href='city.php?city=Noida&compliance=".$SLA_Compliance."'>

转义"符号

echo "<td><a href=\"city.php?city=Noida&ompliance=\"".$SLA_Compliance."\">"

转义" look here