简体   繁体   English

单击谷歌地图中的标记添加信息窗口

[英]adding info window on click on marker in google map

i am using PHP and MYSQL in order to retrieve data from the database and display markers on Google Map API with adding a info windows that includes info retrieved from the database using click-Listener. 我正在使用PHP和MYSQL,以便从数据库中检索数据并在Google Map API上显示标记,并添加一个信息窗口,其中包括使用click-Listener从数据库中检索到的信息。 after i added the info window it doesn't seems it works . 在我添加了信息窗口后,它似乎没有用。

where my error ? 我的错误在哪里?

code : 代码:

<?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();
?>
           <div class="map-popupstring hidden" id="popupstring1">
                <div class="text-center">
                    <h3>title</h3>
                     <p>subinfo</p>
                </div>
            </div>

you must put in the wanted popup in html tags. 您必须在html标记中放入所需的弹出窗口。 and an code in the google maps tags to initialise it. 以及Google Maps标签中的代码对其进行初始化。 and use the data-popupstring-id="#popupstring1" tag to initialise it onclicking the markers. 并使用data-popupstring-id="#popupstring1"标签在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
      });
    }

i solved the question by adding this code : 我通过添加以下代码解决了这个问题:

code: 码:

 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;
                });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM