How to fix "SyntaxError: Unexpected token '<'"

How to fix "SyntaxError: Unexpected token '<'"

好吧,我有一些使用 LAT&LONG 的代码,数据保存在 txt 中,并在 html 中显示为 Google 地图。但是导入 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: Unexpected token '<'" 在第20行。具体在php函数中添加txt的数组。

在第 20 行,您有

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

替换为

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