【问题标题】:How to fix "SyntaxError: Unexpected token '<'" [duplicate]如何修复“SyntaxError:意外的令牌'<'”[重复]
【发布时间】:2019-08-29 11:08:26
【问题描述】:

我有一些代码可以使用 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>

Trows 错误“SyntaxError: Unexpected token '

【问题讨论】:

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


    【解决方案1】:

    在第 20 行,你有

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

    替换为

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

    【讨论】:

    • 把它改成 -> echo 'new google.maps.LatLng("'.$arrayItem.'"),';
    • 谢谢,这部分的另一个问题 var flightPlanCoordinates = [ '' ]; 我得到 "SyntaxError: Unexpected EOF"
    猜你喜欢
    • 2019-01-11
    • 2021-11-23
    • 2021-11-23
    • 2018-01-13
    • 1970-01-01
    • 2017-08-16
    • 2020-10-19
    • 1970-01-01
    • 2011-11-21
    相关资源
    最近更新 更多