在谷歌地图上点击标记时添加信息窗口

3
我正在使用PHP和MYSQL来从数据库中检索数据,并使用click-Listener将标记显示在Google Map API上,添加包含从数据库检索的信息的信息窗口。 添加信息窗口后,它似乎没有起作用。
我的错误在哪里?

代码:

<?php
        /*
        Template Name: MAP2
        */

        get_header();
  ?>


<!DOCTYPE html>
<html>
  <head>
    <title>Custom Markers</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <script async defer
    src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCquey2tCZ32jLJJDEEi2D7_RnXXyj9RTI&callback=initMap">
    </script>
     <style>
      /* Always set the map height explicitly to define the size of the div
       * element that contains the map. */
      #map {
        height: 600px;
      }
      /* Optional: Makes the sample page fill the window. */
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
    </style>

  </head>
  <body>
    <div id="map">
      <div class="map-popupstring hidden" id="popupstring1">
                <div class="text-center">
                    <h3>title</h3>
                     <p>subinfo</p>
                </div>
            </div>

    </div>

    <script>

     var map;
      function initMap() {
        map = new google.maps.Map(document.getElementById('map'), {
          zoom: 8,
          center: new google.maps.LatLng(33.888630, 35.495480),
          mapTypeId: 'roadmap'
        });

        var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/';
        var icons = {
          parking: {
            icon: iconBase + 'parking_lot_maps.png'
          },
          library: {
            icon: iconBase + 'library_maps.png'
          },
          info: {
            icon: iconBase + 'info-i_maps.png'
          }
        };


        var $popup = $("#popupstring1")

        function addMarker(feature) {
          var marker = new google.maps.Marker({
            position: feature.position,
            //icon: icons[feature.type].icon,
            data-popupstring-id: $popup,
            map: map
          });
        }



        var features = [
        <?php
          global $wpdb;
            $prependStr ="";
            foreach( $wpdb->get_results("SELECT siteID, latitude, longitude FROM site_coordinates2", OBJECT) as $key => $row) {
               $latitude = $row->latitude;
               $longitude = $row->longitude;
               $info = $row->siteID;
           echo $prependStr;
       ?>
{
    position: new google.maps.LatLng(<?php echo $latitude; ?>, <?php echo $longitude; ?>),
    type: '<?php echo $info; ?>'
}
<?php
$prependStr =",";
}
?>
        ];

    var infowindow = new google.maps.InfoWindow();

        for (var i = 0, feature; feature = features[i]; i++) {

          addMarker(feature);
        }
}



         </script>


  </body>
</html>

<?php
get_footer();
?>

可能是重复的问题:Google Map弹出窗口内部没有显示任何内容 - duncan
2个回答

0
           <div class="map-popupstring hidden" id="popupstring1">
                <div class="text-center">
                    <h3>title</h3>
                     <p>subinfo</p>
                </div>
            </div>

你必须在HTML标签中放置所需的弹出窗口。并在Google地图标签中使用代码进行初始化。 并使用data-popupstring-id="#popupstring1"标签,在单击标记时进行初始化。

var $popup = $("#popupstring1")

function addMarker(feature) {
      var marker = new google.maps.Marker({
        position: feature.position,
        data-popupstring-id: $popup,
        map: map
      });
    }

我不知道如何将您的答案放入代码中,我是Google地图API的新手。 - Jenny 3akra
将 HTML 放置在 <div id="map"></div> 下方。 - B. Dionys
我尝试了你的答案,但它没有起作用。我将编辑代码并显示编辑后的代码。 - Jenny 3akra
对于这个问题,我不太清楚。抱歉。 - B. Dionys

0

我通过添加以下代码解决了这个问题:

代码:

 var popup = new google.maps.InfoWindow({
                    content: feature,
                    maxWidth: 300
                });

          google.maps.event.addListener(marker, "click", function() {
                    if (currentPopup != null) {
                        currentPopup.close();
                        currentPopup = null;
                    }
                    popup.open(map, marker);
                    currentPopup = popup;
                });
                google.maps.event.addListener(popup, "closeclick", function() {
                    map.panTo(center);
                    currentPopup = null;
                });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接