php - 如何在 Google map 上添加信息窗口和标记

标签 php mysql google-maps marker infowindow

在 WordPress 和 Google Map API 上使用 PHP 和 MYSQL,以便从 MYSQL 数据库检索数据并在 Google map 上显示带有信息窗口的标记。

问题是我无法在 infoWindow 内将从数据库检索到的数据显示为 siteID,其中它是所选表中的字段。

使用以下代码标记不会显示在 map 上

代码:

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

    <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.position.toString(),
// this line that makes the error 
               content: feature.info,
                    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; ?>),
    info:(<?php echo $info;?>),

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



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

          addMarker(feature);
        }
}



         </script>


  </body>
</html>

<?php
get_footer();
?>

最佳答案

用单引号替换信息参数的圆括号。谢谢。

关于php - 如何在 Google map 上添加信息窗口和标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42718455/

相关文章:

javascript - 谷歌地图 vs jquery;在 jsfiddle 中工作,在服务器上停止工作

php - 一次从2个不同的表中获取数据而无需连接php

php - Symfony2 : ClassACE and ObjectACE

php - 有没有一种方法可以检查多个与关系表中最小值的匹配?

php - mySQL 仅选择大于零的列

android - 为什么 map 没有显示在移动设备上?

php - jQuery(或任何网络工具)嵌套表达式生成器

php - 为什么我的数据库备份脚本不能在 php 中运行?

mysql - 一条语句查询两张表?

c# - 用于 asp.net mvc 3 的谷歌地图