2017-03-10 28 views
0

PHPを使用して& MYSQLをWordPressとGoogle Map APIで使用すると、MYSQLデータベースからデータを取得し、Google Mapの情報ウィンドウでマーカーを表示できます。Googleマップで情報ウィンドウとマーカーを追加する方法

問題は、データベースから取得したデータをinfoWindow内に表示できないことです。siteIDここでは、選択したテーブルのフィールドです。以下のコードマーカーを使用して

は、地図上で示したされていない

コード:

<?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(); 
?> 
+0

丸カッコをinfoパラメータに対して一重引用符で置き換えます。ありがとう。 – user1544541

+0

クエリから座標(緯度、経度)を取得していますか? – user1544541

+0

@ user1544541はい回答がありがとうございました –

答えて

0

は、infoパラメータに対する単一引用符で丸括弧を交換してください。ありがとう。

+0

これらのアイコンの作成方法は、通常のマーカーの代わりに表示されますか? 私はアイコンを持っていますが、何も必要なアイコンとして表示されません –

+0

詳細を教えてください。ありがとう。 – user1544541

+0

私は、コードでそれを行うには、通常のマーカーの代わりにアイコンをしたいですか? javascriptまたはPHP? –

関連する問題