【问题标题】:Google Map pop -up windows doesn't show anything inside it谷歌地图弹出窗口不显示里面的任何东西
【发布时间】:2017-07-29 03:28:41
【问题描述】:

我在带有 Google Map API 的 WordPress 上使用 PHP MYSQL,代码从 MYSQL 数据库中检索数据,并根据数据库中的现有坐标在地图上显示标记。它还会在单击侦听器时显示一个信息窗口。

问题在于 infow 窗口中没有显示任何数据。

谁能告诉我错误在哪里?

代码:

<?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=**********&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>

    <script>

     var map,currentPopup;
      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'
          }
        };




        function addMarker(feature) {
          var marker = new google.maps.Marker({
            position: feature.position,
            //icon: icons[feature.type].icon,

            map: map
          });

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



        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; ?>),

}
<?php
$prependStr =",";
}
?>
        ];



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

          addMarker(feature);
        }
}



         </script>


  </body>
</html>

<?php
get_footer();
?>

【问题讨论】:

  • 你能看看你的浏览器的开发控制台,看看地图的API是否抛出任何错误?另外,尝试调整浏览器的大小,如果地图显示,则需要添加一些额外的代码来修复它。
  • 您的代码生成了一个javascript错误:InvalidValueError: setContent: not a string; and [object Object],因为正如@duncan 所观察到的,feature 不是content: feature, 这一行中的字符串。您希望什么内容出现在信息窗口中?

标签: php mysql google-maps google-maps-api-3


【解决方案1】:

如果我没看错你的代码,那么你的一系列功能看起来像:

features = [
  {position: new google.maps.LatLng(1, 2)},
  {position: new google.maps.LatLng(3, 4)},
  // etc...
];

即该数组包含仅具有 position 属性的对象。所以当你这样做时,你正确地提到了这一点:

position: feature.position,

但是,当您尝试使用以下方式设置信息窗口内容时:

new google.maps.InfoWindow({
    content: feature,
    maxWidth: 300
})

这是行不通的,因为content 属性是一个字符串,而不是一个 JS 对象。您需要在那里指定一些文本。如果你只是想显示坐标,你可以这样做:

new google.maps.InfoWindow({
    content: feature.position.toString(),
    maxWidth: 300
})

【讨论】:

  • 我想在信息窗口中显示一些从 MYSQL 数据库中检索到的信息,位于坐标旁边,但是当我在此行下添加一些内容时,地图没有显示 position: new google.maps.LatLng(&lt;?php echo $latitude; ?&gt;, &lt;?php echo $longitude; ?&gt;),
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-15
  • 1970-01-01
  • 2011-02-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多