如何修复“ SyntaxError:意外令牌'<'”

时间:2019-04-08 10:15:08

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

好吧,我有一些代码可以使用LAT&LONG,数据保存在txt中,并以html的形式显示为Google Map。但是导入txt的php函数完全无效。知道问题出在哪里吗?

我试图在其他机器和系统上运行,在rasp中安装了Nginx

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>LOCATION</title>
    <style>
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?key="></script>
    <?php $array = explode("\n", file_get_contents('locations.txt')); ?>
    <script>
    function initialize() {
    var mapOptions = {
    zoom: 20,
    center: new google.maps.LatLng(<?php echo $array[0]; ?>),
    mapTypeId: google.maps.MapTypeId.HYBRID
    };


    var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

    var flightPlanCoordinates = [

<?php foreach ($array as $arrayItem) { 
echo 'new google.maps.LatLng('.$arrayItem.'),'; 
} ?>
                                ];

    var flightPath = new google.maps.Polyline({
        path: flightPlanCoordinates,
        geodesic: true,
        strokeColor: '#FF0000',
        strokeOpacity: 1.0,
        strokeWeight: 2
    });
    flightPath.setMap(map);
    }
    google.maps.event.addDomListener(window, 'load', initialize);
    </script>
    </head>
    <body>
        <div id="map-canvas"></div>
    </body>
</html>

引发错误“ SyntaxError:意外令牌'<'” 在第20行中。特别是在php函数中添加txt数组。

1 个答案:

答案 0 :(得分:-1)

第20行,您有

center: new google.maps.LatLng(<?php echo $array[0]; ?>),

替换为

center: new google.maps.LatLng('<?php echo $array[0]; ?>'),