javascript - 在谷歌地图中点击标记添加信息窗口

标签 javascript php mysql google-maps

我正在使用 PHP 和 MYSQL 来从数据库中检索数据并在 Google Map API 上显示标记,并添加一个信息窗口,其中包含使用 click-Listener 从数据库中检索到的信息。 添加信息窗口后,它似乎不起作用。

我的错误在哪里?

代码:

<?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>

您必须在 html 标签中放入想要的弹出窗口。和谷歌地图标签中的代码来初始化它。 并使用 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
      });
    }

关于javascript - 在谷歌地图中点击标记添加信息窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42627471/

相关文章:

javascript - 闭包中变量引用的意外行为

php - MySQL:使用 while 循环链接未显示在正确的列中

php - 查询具有多个值的多个元键的 Wordpress 帖子

javascript - TypeError,captureStream 不是函数

javascript - 移动 Angular ng-view 不适用于 PhoneGap 应用程序

php - 如何确保在一次 session 中评分的图像不会再次出现供用户评分?

php - 更新php&mysql : undefined index

MySQL - 一个表上的行删除不会更新另一表上的关系

mysql 分区由 varchar - 随机行为?

页面加载时未调用 JavaScript 函数