【问题标题】:PHP & Javascript Issue - SyntaxError: Cannot declare a const variable twice: 'map' [closed]PHP & Javascript 问题 - SyntaxError:不能两次声明 const 变量:'map' [关闭]
【发布时间】:2021-05-11 11:08:12
【问题描述】:

我正在尝试使用 JSON 文件中的 long/lat 列表填充 google 地图并遇到此问题,我们将不胜感激!

[错误] SyntaxError: 不能两次声明 const 变量:'map'。

[错误] 未处理的 Promise Rejection: InvalidValueError: initMap is not a function

    <!-- Async script executes immediately and must be after any DOM elements used in callback. -->
        <script
          src="https://maps.googleapis.com/maps/api/js?key=MYAPIKEY&callback=initMap&libraries=&v=weekly"
          async
        ></script>
        <script>
        
            // Initialize and add the map
            function initMap() {
              // The location of Uluru
              const uluru = { lat: -25.344, lng: 131.036 };
              // The map, centered at Uluru
              const map = new google.maps.Map(document.getElementById("map"), {
                zoom: 5,
                center: uluru,
              });
              // The marker, positioned at Uluru
              
              <?php
                $data = json_decode($eaterJSON,true);
                $data = array_values( array_unique( $data, SORT_REGULAR ) );
                $clean = json_encode($data);
                // print_r($clean);
              
                foreach($data as $key => $value) {
                ?>
                
                const map = new google.maps.Marker({
                    position: {lat: <?php echo $value['lat'] ?>, lng: <?php echo $value['lon'] ?>},
                    map: map,
                  });
                  
                  
                <?php
                }
                ?>
                
            }

    </script>

【问题讨论】:

    标签: javascript php google-maps


    【解决方案1】:

    您需要将第二个 ma​​p const 重命名为 ma​​rker

    发件人:const map = new google.maps.Marker({ 至:const marker = new google.maps.Marker({

    更新: 等一下!您使用 PHP 循环生成 JS 代码(标记),这会导致多个相同的变量名!请详细了解(多个)标记创建!

    在这种情况下,您无需为变量分配标记,只需在循环中创建标记:

        <!-- Async script executes immediately and must be after any DOM elements used in callback. -->
        <script
          src="https://maps.googleapis.com/maps/api/js?key=MYAPIKEY&callback=initMap&libraries=&v=weekly"
          async
        ></script>
        <script>
        
            // Initialize and add the map
            function initMap() {
              // The location of Uluru
              const uluru = { lat: -25.344, lng: 131.036 };
              // The map, centered at Uluru
              const map = new google.maps.Map(document.getElementById("map"), {
                zoom: 5,
                center: uluru,
              });
              // The marker, positioned at Uluru
              
              <?php
                $data = json_decode($eaterJSON,true);
                $data = array_values( array_unique( $data, SORT_REGULAR ) );
                $clean = json_encode($data);
                // print_r($clean);
              
                foreach($data as $key => $value) {
                ?>
                
                new google.maps.Marker({
                    position: {lat: <?php echo $value['lat'] ?>, lng: <?php echo $value['lon'] ?>},
                    map: map,
                  });
                  
                  
                <?php
                }
                ?>
                
            }
    
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-26
      相关资源
      最近更新 更多