2017-08-04 124 views
-1

我遇到問題。這是我的地圖代碼。它運作良好。它動態地顯示地圖圖標。現在我想添加地圖圖標懸停名稱動態。我怎樣才能做到這一點?地圖圖標名稱動態添加

<script> 
     var infowindows = []; 
     var map; 
     function initMap() { 
      map = new google.maps.Map(document.getElementById('map'), { 
       zoom: 7, 
       center: new google.maps.LatLng(<?php echo $flag[0]->lat; ?>, <?php echo $flag[0]->lang; ?>), 
       mapTypeId: 'roadmap' 
      }); 
      var icons = { 
       parking: { 
       icon: '<?php echo get_template_directory_uri(); ?>/img/icon332.png' 
       } 
      }; 
      var features = [ 
      <?php 
      foreach($map as $map) 
      { 
      ?> 
       { 
        position: new google.maps.LatLng(<?php echo $map->lat;?>, <?php echo $map->lang;?>), 
        type: 'parking' 
       }, 
      <?php 
      } 
      ?> 

      ]; 

      features.forEach(function(feature) { 
       var marker = new google.maps.Marker({ 
       position: feature.position, 
       //position: currentLatLng, 
       icon: icons[feature.type].icon, 
       map: map 
       }); 
      }); 
     } 
    </script> 
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDr-oU870bxiB7TJcrlfAtN9HjEvZzjdGI&callback=initMap"></script> 

回答

0

這是解決方案:

var map; 
google.maps.event.addDomListener(window, "load", function() { 
    var map = new google.maps.Map(document.getElementById("map"), { 
    center: new google.maps.LatLng(<?php echo $flag[0]->lat; ?>, <?php echo $flag[0]->lang; ?>), 
    zoom: 8, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    }); 
    var infoWindow = new google.maps.InfoWindow(); 
    function createMarker(options, html) { 
    var marker = new google.maps.Marker(options); 
    if (html) { 
     google.maps.event.addListener(marker, "click", function() { 
     infoWindow.setContent(html); 
     infoWindow.open(options.map, this); 
     }); 
    } 
    return marker; 
    } 

    <?php 
    foreach($map as $map) 
    { 
    ?> 
    var marker0 = createMarker({ 
    position: new google.maps.LatLng(<?php echo $map->lat;?>, <?php echo $map->lang;?>), 
    map: map, 
    icon: "<?php echo get_template_directory_uri(); ?>/img/icon332.png" 
    }, "<?php echo $map->hotel; ?>"); 
    <?php 
    } 
    ?> 

});