<! - ?php - >上出现意外的令牌错误

时间:2014-04-12 23:48:38

标签: javascript php google-maps

我正在建立一个网页,从数据库中检索一系列纬度和经度,然后在谷歌地图上显示这些标记作为标记。除了收到Uncaught SyntaxError:Unexpected token&lt;之外,它一切正常。在javascript里面打开php标签。任何人都可以看到我为什么会收到此错误并提供一种方法来解决它?

以下是有问题的代码:

    <script type="text/javascript">
//<![CDATA[
var map;
var bounds = new google.maps.LatLngBounds ();
  function initialize() {
    var mapOptions = {
      center: new google.maps.LatLng(-34.397, 150.644),
      zoom: 8
    };
    map = new google.maps.Map(document.getElementById("map-canvas"),
        mapOptions);
  }

//HERE'S WHERE THE ERROR OCCURS  
<?php
  while ($row = $results->fetch_array())
    echo "addMarker(".$row['latitude'].", ".$row['longitude'].", ".$row['time'].", map);";
?>
    function addMarker(lat, lng, time, map) {
        var latLng = new google.maps.LatLng(lat, lng);
        var marker = new google.maps.Marker({
            position: latLng,
            map: map,
            draggable: false,
        });

        bounds.extend(latLng);

        var contentString = 'Time: ' + time + '\n' + 'Latitude: ' + lat + '\n' + 'Longitude: ' + lng;

        var infowindow = new google.maps.InfoWindow({
            content: contentString
        });

        google.maps.event.addListener(marker, 'click', function() {
            infowindow.open(map,marker);
        });

        return marker;
    }

  map.fitBounds(bounds);
  google.maps.event.addDomListener(window, 'load', initialize);
  //]]>
</script>

1 个答案:

答案 0 :(得分:2)

这是因为CDATA字面意思是每个角色。

<?php会生成错误,因为解析器会将&lt; 解释为新XML元素的开头。

相关问题