Google Maps API仅显示一个infowindow-loop问题

时间:2015-04-11 19:15:15

标签: javascript google-maps loops google-maps-api-3

所有引脚都应该落在它们应该的位置。但是,所有引脚共享相同的信息框。很难确定创建地图的方法,以显示来自json feed的所有引脚并显示自定义信息窗口。

        $.ajax({
            url: '/search?cct=US&&cty='+searchloc+'&add=&mobilefeed=true',
            dataType: 'json',
            success: function(data, status){


                var infowindow; 

                var count = 1;

                function newinfo(marker,locationname,thumb,address,itemid){

                 var contentString = '<a href="search_listing.html?propid='+itemid+'&proptitle='+locationname+'" class="item-link" ><div id="infocontent"><img src="'+thumb+'" width="75px" style="float:left;margin-right:10px;">'+

                          '<b>'+locationname+'</b>'+
                          '<br>'+address+''+

                          '</div></a>';


                         infowindow = new google.maps.InfoWindow({
                                content: contentString
                            });


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

                $.each(data, function(i,item){ 

                    if (count==1){
                         var center = new google.maps.LatLng(item.lat, item.long);
                        // using global variable:
                        map.panTo(center);
                    }
                    var locationname = item.title;
                    var imagethumb = '/listing/'+item.id+'/?mobilefeedview=true&img=true';



                    marker = new google.maps.Marker({
                    position: new google.maps.LatLng(item.lat, item.long),
                    title: locationname,
                    map: map
                    });

                    newinfo(marker,locationname,imagethumb,item.address,item.id);

                    count = count + 1;

                });



            },
            error: function(){
                alert('There was an error loading the data. 928');
            }



        });



    var searchmapOptions = {
        center: new google.maps.LatLng(32.7150771, -117.1642001),
        zoom: 12,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };


var map = new google.maps.Map(document.getElementById("searchmap"), searchmapOptions);

1 个答案:

答案 0 :(得分:1)

单击标记时,您已为每个数据点调用newinfo,变量infowindow包含最后一个标记的值。如果将infowindow作为自定义数据存储在标记中,那么您将找到正确的数据。

marker.infowindow = new google.maps.InfoWindow({
  content: contentString
});

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