简体   繁体   English

Google Maps API v3 - 标记全部共享相同的InfoWindow

[英]Google Maps API v3 - Markers All Share The Same InfoWindow

I've been digging around everywhere and I can't seem to figure this out. 我一直在四处挖掘,我似乎无法弄清楚这一点。 It's driving me crazy! 这让我疯狂! I'm a newbie to javascript in general, so I can't quite put a finger on the translation that would fix my issue. 我一般都是javascript的新手,所以我不能完全理解可以解决我的问题的翻译。 I noticed that a lot of people have this problem, but they all seem to use more advanced(or just confusing) code than I. Anyway, here goes! 我注意到很多人都有这个问题,但他们似乎都使用比我更高级(或者只是令人困惑)的代码。无论如何,这里有!

I've been having the problem where all of my markers share the same content. 我一直遇到的问题是我的所有标记共享相同的内容。

function initialize() {
var myOptions = {
    center: new google.maps.LatLng(34.151271, -118.449537),
    zoom: 9,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    mapTypeControl: false,
    streetViewControl: false,
    panControl: false,
    zoomControl: true,
    zoomControlOptions: { style: google.maps.ZoomControlStyle.SMALL },
};

var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

setMarkers(map, clubs);

}

var clubs = [
['Poop', 34.223868, -118.601575, 'Dookie'],
['Test Poop', 34.151271, -118.449537, 'Test Business']
];

function setMarkers(map, locations) {
var image = new google.maps.MarkerImage('images/image.png',
    new google.maps.Size(25, 32),
    new google.maps.Point(0,0),
    new google.maps.Point(0, 32)
);
var shape = {
coord: [1, 1, 1, 20, 18, 20, 18 , 1],
type: 'poly'
};
for (var i = 0; i < locations.length; i++) {
var club = locations[i];
var myLatLng = new google.maps.LatLng(club[1], club[2]);
var infowindow = new google.maps.InfoWindow();
var marker = new google.maps.Marker({
    position: myLatLng,
    map: map,
    icon: image,
    shape: shape,
    title: club[0],
});
google.maps.event.addListener(marker, 'click', function(){
    infowindow.setContent(club[3]);
    infowindow.open(map, this);
});
}
}

I know I'm crappy, but someone please help me! 我知道我很糟糕,但有人请帮助我! :P :P

The problem is because you're setting the event listener for the marker click within a loop. 问题是因为您在循环中为标记点击设置事件侦听器。 So all the markers end up only getting the content for the last of your markers. 因此,所有标记最终只能获取最后一个标记的内容。 Try this instead. 试试这个。 Create a new global function: 创建一个新的全局函数:

function bindInfoWindow(marker, map, infowindow, html) {
    marker.addListener('click', function() {
        infowindow.setContent(html);
        infowindow.open(map, this);
    });
} 

Then within your loop, replace this: 然后在你的循环中,替换为:

google.maps.event.addListener(marker, 'click', function(){
    infowindow.setContent(club[3]);
    infowindow.open(map, this);
});

with this: 有了这个:

// add an event listener for this marker
bindInfoWindow(marker, map, infowindow, club[3]); 

When setting the marker object (var marker = new ...) change this line: "title: club[0]," to "title: club[i],". 设置标记对象(var marker = new ...)时,将此行更改为:“title:club [0]”,“title:club [i],”。 Yes, just change the 0 to i. 是的,只需将0更改为i。

That should solve the problem. 那应该可以解决问题。

Try this link for a tutorial on Google Maps API with examples. 试试这个链接,获取有关Google Maps API的教程和示例。

http://code.google.com/apis/maps/documentation/javascript/tutorial.html http://code.google.com/apis/maps/documentation/javascript/tutorial.html

It should be very easy and helpful. 它应该非常简单和有用。

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

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