doukun0888 2014-04-12 23:48
浏览 8
已采纳

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

I'm building a webpage that retrieves a series of latitudes and longitudes from a database and then displays these as markers on a google map. It's all working fine, apart from I receive an Uncaught SyntaxError: Unexpected token < on the opening php tag inside the javascript. Can anyone see why I'm getting this error and offer a way to get past it?

Here's the code in question:

    <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 + '
' + 'Latitude: ' + lat + '
' + '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条回答 默认 最新

  • duancheng6221 2014-04-12 23:50
    关注

    That's because of CDATA It takes every character literally.

    <?php will generate an error because the parser interprets < as the start of a new XML element.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画