简体   繁体   English

谷歌地图标记-Infowindow

[英]Google maps marker - infowindow

I need to in InfoWindow show match.offer.text. 我需要在InfoWindow中显示match.offer.text。 Each marker has a different match.offer.text. 每个标记都有一个不同的match.offer.text。

This is my code: 这是我的代码:

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

Event triggered after clicking on the marker: 单击标记后触发事件:

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

Please, help me. 请帮我。

The content is applied to the marker on Open therefore your code will apply the content in last item in your loop to all markers, in your openWindow function add the content to a single infoWindow object ie 内容将应用到Open上的标记,因此您的代码会将循环中最后一项中的内容应用于所有标记,在openWindow函数中,将内容添加到单个infoWindow对象,即

Also the Maps API has its own event wrapper for the click event 此外,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