简体   繁体   中英

Google maps marker - infowindow

I need to in InfoWindow show match.offer.text. Each marker has a different 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

Also the Maps API has its own event wrapper for the click event

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM