2016-01-10 131 views
0

我需要在InfoWindow中顯示match.offer.text。 每個標記都有不同的match.offer.text。Google maps marker - infowindow

這是我的代碼:點擊標記後

var markerImageURL, lat, lng; 
if (myoffer) { 
    markerImageURL = 'assets/img/markers/marker_my.png'; 
    lat = match.lat; 
    lng = match.lng; 
} else { 
    markerImageURL = 'assets/img/markers/marker_' + match.strength + '.png'; 
    lat = match.offer.lat; 
    lng = match.offer.lng; 
} 

var marker = new google.maps.Marker({ 
    position: new google.maps.LatLng(lat, lng), 
    map: window.googleMap, 
    icon: { 
     size: new google.maps.Size(54,56), 
     url: markerImageURL 
    }, 
    draggable: false, 
    visible: true 
}); 

var infowindow = new google.maps.InfoWindow({ 
    content: match.offer.text, 
    maxWidth: 300 
}); 

window.googleMapMarkers.push(marker); 

if(!myoffer) { 
    window.MVC.Events.GoogleMap.prototype.showInfoWindow(marker, infowindow, match); 
} 

事件觸發:

marker.addListener('click', function() { 
    infowindow.open(window.googleMap, marker); 
} 

請幫助我。

+1

您是否在開發人員工具控制檯中看到任何錯誤?你的代碼目前做什麼? –

+0

請提供一個[最小,完整,已測試和可讀的示例](http://stackoverflow.com/help/mcve)來說明您的問題。 – geocodezip

+0

[Google Maps JS API v3 - 簡單多標記示例]的可能重複(http://stackoverflow.com/questions/3059044/google-maps-js-api-v3-simple-multiple-marker-example) – geocodezip

回答

1

的內容應用到開放的標誌,因此您的代碼將在循環應用中最後一項內容,所有標記,在你的openWindow功能的內​​容添加到一個單一的信息窗口對象即

另外,地圖API有其自己的事件封裝器,用於單擊事件

function initMarkers(){ 
    //create markers 
    var marker = new google.maps.Marker(); 

    google.maps.event.addListener(marker, 'click', openInfoWindow(marker, i)); 
} 

var infowindow = new google.maps.InfoWindow(); 
function openInfoWindow(marker,index){ 
     return function(e) { 

      //Close other info window if one is open 
      if (infowindow) { 
       infowindow.close(); 
      } 

      var content = marker.offer.text; 

      infowindow.setContent(content); 

      setTimeout(function() { 
       infowindow.open(map, marker); 
      }, 200); 
     } 
} 
+0

謝謝!現在它工作... – webmazz