簡體   English   中英

谷歌地圖標記-Infowindow

[英]Google maps marker - infowindow

我需要在InfoWindow中顯示match.offer.text。 每個標記都有一個不同的match.offer.text。

這是我的代碼:

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);
}

請幫我。

內容將應用到Open上的標記,因此您的代碼會將循環中最后一項中的內容應用於所有標記,在openWindow函數中,將內容添加到單個infoWindow對象,即

此外,Maps 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);
        }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM