简体   繁体   English

谷歌地图3:当任何标记点击相同的infoWindow打开时

[英]Google Maps 3 : when any marker clicked same infoWindow opens up

I have several markers on my google map. 我的谷歌地图上有几个标记。 Each has text that should show when it is clicked in the form of an infowindow. 每个都有文本,应该显示何时以infowindow的形式点击它。 The code for all this is below. 所有这些的代码如下。 Hwever, the problem is that, when I click any marker on the map, its always the same marker that the map zooms in on and the same marker's infoWindow pops up. Hwever,问题在于,当我点击地图上的任何标记时,它总是与地图放大的标记相同,并且弹出相同标记的infoWindow。 No matter where I click its always the same. 无论我在哪里点击它总是一样的。

Wondering if there is something in plain sight that Im doing wrong. 想知道我是否做错了。 Could someone give me some advice? 有人可以给我一些建议吗?

  function getlistings()  
   {  
     $.getJSON('list/listings.json', processlistings);  
   }  



function processlistings(data)   
{  
    listings = data;  
    markersArray = [];  
    for( i = 0; i < listings.length; i++)   
    {  
        var listing = listings[i];  
        var markerLatLng = new google.maps.LatLng(parseFloat(listing["latitude"]), parseFloat(listing["longitude"]));  
        marker = new google.maps.Marker({  
            position : markerLatLng,  
            title: listing['name'],  
            map : map  
        });  

    var infowindow = new google.maps.InfoWindow(  
        {  
          content: listing['name'],  
          position: markerLatLng  
        });  

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

    });  

    markersArray.push(marker);  
}  

} }

When you loop through your data, you are creating an InfoWindow many times over. 当您遍历数据时,您将多次创建InfoWindow At the end of the loop, you have one InfoWindow at markerLatLng . 在循环结束时,您在markerLatLng处有一个InfoWindow

When the marker is clicked, infowindow.open() is called, which uses the infowindow at markerLatLng . 单击标记时,将调用markerLatLng infowindow.open() ,它在markerLatLng使用markerLatLng

You need to use a function closure, as demonstrated at http://www.geocodezip.com/v3_MW_example_map1.html 您需要使用函数闭包,如http://www.geocodezip.com/v3_MW_example_map1.html所示。

//loop through markers, but create each with a helper function
          var point = new google.maps.LatLng(43.82589,-79.10040);
          var marker = createMarker(point,'Some stuff to display in the<br>Third Info Window')

    function createMarker(latlng, html) {
        var contentString = html;
        var marker = new google.maps.Marker({
            position: latlng,
            map: map,
            zIndex: Math.round(latlng.lat()*-100000)<<5
            });

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

as you loop through the listings, you're overwriting the infowindow var contents. 当您遍历列表时,您将覆盖infowindow var内容。 instead you need to change the click to set the contents to this.title 相反,您需要更改单击以将内容设置为this.title

var infoWindow = new google.maps.InfoWindow({content: 'Loading...'});;    
function processlistings(data)   
    {  
        listings = data;  
        markersArray = [];  
        for( i = 0; i < listings.length; i++)   
        {  
            var listing = listings[i];  
            var markerLatLng = new google.maps.LatLng(parseFloat(listing["latitude"]), parseFloat(listing["longitude"]));  
            marker = new google.maps.Marker({  
                position : markerLatLng,  
                title: listing['name'],  
                map : map  
            });  

        markersArray.push(marker);  
        } 
        for (var i = 0; i < markersArray.length; i++) {
            var marker = markersArray[i];
            google.maps.event.addListener(marker, 'click', function () {
            infoWindow.setContent(this.title);
            infoWindow.open(map, this);
            });
        }
    }

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

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