简体   繁体   English

Google Maps API信息窗口均具有相同的内容

[英]Google Maps API infowindows all have the same content

I have the old infowindows in a loop problem where the content for the last loop is showing in all infowindows. 我有一个循环问题中的旧信息窗口,其中最后一个循环的内容显示在所有信息窗口中。 Yes I know there are several questions about this already on Stack Overflow but none of them seem to work for me. 是的,我知道在Stack Overflow上已经有关于此的几个问题,但是似乎没有一个对我有用。

This is my JavaScript: 这是我的JavaScript:

var map;
var geocoder;

$(function () {

    var mapOptions = {
        zoom: startZoom,
        center: new google.maps.LatLng(startLat, startLng)
    }
    var marker, i;

    $('#map-canvas').height($('#map-canvas').width() / 2);
    var mapOptions = {
        zoom: startZoom,
        center: new google.maps.LatLng(startLat, startLng)
    }
    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

    if ( ! isAddress && $('#country').val() > 0) {
        geocoder = new google.maps.Geocoder();
        geocoder.geocode({'address': $('#country').find('option:selected').text()}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                map.setCenter(results[0].geometry.location);
                map.fitBounds(results[0].geometry.viewport);
            }
        });
    }

    for (i = 0; i < distributors.length; i++) {
        var $distributor = distributors[i];
        var marker = new google.maps.Marker({
            position: new google.maps.LatLng($distributor.latitude, $distributor.longitude),
            map: map
        });
        var infowindow = new google.maps.InfoWindow();
        var html = '<div class="container-fluid" style="width: 300px">\
            <h1 class="row-fluid">\
                '+($distributor.logo ? '<div class="span3"><img src="'+$distributor.logo+'" style="width: 100%"></div>' : '')+'\
                <span class="span9">'+$distributor.name+'</span>\
            </h1>\
            <div class="row-fluid">\
                <div class="span6">'+$distributor.address+'<br>'+$distributor.postcode+'</div>\
                <div class="span6">'+($distributor.url ? '<a href="'+$distributor.url+'">'+$distributor.url+'</a>' : '')+'<br>'+$distributor.contactNumber+'</div>\
            </div>\
        </div>';

        google.maps.event.addListener(marker, 'click', (function(marker, i) {
            return function() {
                infowindow.setContent(html);
                infowindow.open(map, marker);
            }
        })(marker, i));
    }

})

So far I've tried this answer , but the variables mentioned don't match what I have and I couldn't make them match up, it just didn't work. 到目前为止,我已经尝试了这个答案 ,但是提到的变量与我的变量不匹配,我无法使它们匹配,这只是行不通。

I've also tried this answer , but instead of getting different content it removed all but one of my markers. 我也尝试过这个答案 ,但是除了获得了不同的内容,它删除了我的标记之一。

What am I doing wrong? 我究竟做错了什么? Can someone please help me sort this out? 有人可以帮我解决这个问题吗?

Try this after creating infoWindow and html objects: 创建infoWindow和html对象后,请尝试以下操作:

marker.html = html;

Then build your event listener like this: 然后像这样构建事件监听器:

google.maps.event.addListener(marker, 'click', function () {
                    infoWindow.setContent(this.html);
                    infoWindow.open(map, this);
                });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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