【问题标题】:Google Maps API Multiple Marker InfoWindowContent showing same dataGoogle Maps API Multiple Marker InfoWindowContent 显示相同的数据
【发布时间】:2017-07-12 23:11:38
【问题描述】:

我已经对数据进行了安慰,这很好,我看到不同的名称得到了正确的安慰。多个标记也正在创建中,但在 InfoWindow 上,它只显示每个标记的最后一行的数据。

<?php

  include 'connect.php';

  $locations=array();
  $apikey = "APIKEY";
  $query = $db->query('SELECT * FROM fields');
  while( $row = $query->fetch_assoc() ){

            $name = $row['field_name'];
            $longitude = $row['field_longitude'];                              
            $latitude = $row['field_latitude'];
            $owner = $row['field_owner'];
            $incharge = $row['field_incharge_name'];
            $contact_number = $row['contact_number'];
            $field_address = $row['field_address'];
            $field_pitch_length = $row['field_pitch_length'];
            $field_pitch_breadth = $row['field_pitch_breadth'];
            $ground_busy_hours_per_week = $row['ground_busy_hours_per_week'];
            $locations[]=array('field_name'=>$name,'lat'=>$latitude,'lng'=>$longitude, 'owner'=>$owner, 'incharge'=>$incharge, 'contact_number'=>$contact_number, 'field_address'=>$field_address, 'field_pitch_length'=>$field_pitch_length, 'field_pitch_breadth'=>$field_pitch_breadth, 'ground_busy_hours_per_week'=>$ground_busy_hours_per_week);
        }
        $markers = json_encode($locations);

?>
  <body>
    <div id="map"></div>

    <script>
        <?php
            echo "var markers=$markers;\n";
        ?>
        function initMap() {
            var map = new google.maps.Map(document.getElementById('map'), {
              zoom: 10,
              center: {lat: 15.3489395, lng: 73.7347356},
              mapTypeId: 'roadmap'
            });                               

            for(i = 0; i < markers.length; i++) {
          var infoWindow = new google.maps.InfoWindow(), marker;
                infoWindowContent = '<div class="info_content">' +
                '<h4>Field Name: </h4><p>'+' '+markers[i].field_name+'</p><br>' +
                '<h4>Owner: </h4><p>'+' '+markers[i].owner+'</p><br>' +
                '<h4>Incharge: </h4><p>'+' '+markers[i].incharge+'</p><br>' +
                '<h4>Contact No: </h4><p>'+' '+markers[i].contact_number+'</p><br>' +
                '<h4>Field Address: </h4><p>'+' '+markers[i].field_address+'</p><br>' +
                '<h4>Pitch Length: </h4><p>'+' '+markers[i].field_pitch_length+'</p><br>' +
                '<h4>Pitch Breadth: </h4><p>'+' '+markers[i].$field_pitch_breadth+'</p><br>' +
                '<h4>Busy Hours per Week: </h4><p>'+' '+markers[i].ground_busy_hours_per_week+'</p><br>' +
                '</div>';
                lat = parseFloat(markers[i].lat);
                lng = parseFloat(markers[i].lng);
              var position = new google.maps.LatLng(lat, lng);
              marker = new google.maps.Marker({
                    position: position,
                    map: map
              });


            google.maps.event.addListener(marker, 'click', (function(marker) {
                return function() {
                    infoWindow.setContent(infoWindowContent);
                    infoWindow.open(map, marker);
                }
            })(marker));
            }
            google.maps.event.addDomListener(window, 'load', initMap);
        }   


    </script>
    <script src="https://maps.googleapis.com/maps/api/js?key=KEY&callback=initMap">
    </script>
  </body>

抱歉不能共享数据库,也许你可以使用虚拟数据进行测试。我不知道为什么它只显示最后一行的数据。

感谢您的帮助。

【问题讨论】:

    标签: javascript php google-maps-api-3 google-maps-markers


    【解决方案1】:

    我认为您还需要将“infoWindowContent”传递给闭包。

    google.maps.event.addListener(marker, 'click', (function(marker, infoWindowContent) {
                return function() {
                    infoWindow.setContent(infoWindowContent);
                    infoWindow.open(map, marker);
                }
            })(marker, infoWindowContent));
    

    【讨论】:

    • 是的,就是这样。我错过了这么愚蠢的事情!谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-31
    • 1970-01-01
    • 2019-10-25
    • 2020-11-07
    • 2014-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多