显示在从MYSQL数据库检索到的Google Map值的信息窗口的内容中

时间:2017-03-09 09:31:47

标签: php mysql google-maps google-maps-markers infowindow

我正在使用PHP& wordpress上的MYSQL,用于从数据库中检索包含Google Map上的坐标和显示标记的数据。

标记显示在地图上,信息窗口包含坐标。

我想从数据库中检索更多信息并将其包含在信息窗口中

infow windows

在调试模式下,当我取消注释时,它会显示我要添加到信息窗口的信息,标记将不会显示。

 <?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,
// i need to add here the info 
            //icon: icons[feature.type].icon,
            map: map
          });

          var popup = new google.maps.InfoWindow({
                    content: feature.position.toString(),
     //or here   the 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();
?>

enter image description here

其中AM001是MYSQL数据库中的第一个站点ID

1 个答案:

答案 0 :(得分:0)

假设您希望我们将sietID作为信息

$ export CFLAGS=\""-I/usr/lib/..."\"  # escaped double quotes make them to be included in the string.
$ make

然后在你的var弹出窗口中你应该添加信息文本

            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 =",";
  }
  ?>
          ];