admin管理员组文章数量:1308212
I have google maps on my website that displays a list of locations around the UK. But the info window displays the same record for each individual record. My code is underneath. Any reason why?
var locations = [[52.478899, -1.894055, 'test 1', '87', 'Name: test 1'],
[52.479474, -1.895946, 'test 2', '88', 'Name: test 2 '],
[52.477082, -1.893929, 'test 3', '89', 'Name: test 3 ']];
function initialize() {
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(52.528680, -1.839480),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var bounds = new google.maps.LatLngBounds();
for (var i = 0; i < locations.length; i++) {
var curLoc = locations[i];
myLatLng = new google.maps.LatLng(curLoc[0], curLoc[1]);
var beachMarker = new google.maps.Marker({
position: myLatLng,
map: map,
title: curLoc[2],
data: curLoc[4], //add content to the Marker-object
zIndex: 50 - i
});
var infowindow = new google.maps.InfoWindow({
content: curLoc[4]
});
google.maps.event.addListener(beachMarker, 'click', (function() {
infowindow.setContent(beachMarker.data);
infowindow.setPosition(beachMarker.position);
infowindow.open(map);
}));
bounds.extend(myLatLng);
map.fitBounds(bounds);
}
}
google.maps.event.addDomListener(window, 'load', initialize);
UPDATE: I changed my code but the problem still presists but now clicking on a marker focuses on the last marker not just displays the last markers text.
Here is a sample url .html
I have google maps on my website that displays a list of locations around the UK. But the info window displays the same record for each individual record. My code is underneath. Any reason why?
var locations = [[52.478899, -1.894055, 'test 1', '87', 'Name: test 1'],
[52.479474, -1.895946, 'test 2', '88', 'Name: test 2 '],
[52.477082, -1.893929, 'test 3', '89', 'Name: test 3 ']];
function initialize() {
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(52.528680, -1.839480),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var bounds = new google.maps.LatLngBounds();
for (var i = 0; i < locations.length; i++) {
var curLoc = locations[i];
myLatLng = new google.maps.LatLng(curLoc[0], curLoc[1]);
var beachMarker = new google.maps.Marker({
position: myLatLng,
map: map,
title: curLoc[2],
data: curLoc[4], //add content to the Marker-object
zIndex: 50 - i
});
var infowindow = new google.maps.InfoWindow({
content: curLoc[4]
});
google.maps.event.addListener(beachMarker, 'click', (function() {
infowindow.setContent(beachMarker.data);
infowindow.setPosition(beachMarker.position);
infowindow.open(map);
}));
bounds.extend(myLatLng);
map.fitBounds(bounds);
}
}
google.maps.event.addDomListener(window, 'load', initialize);
UPDATE: I changed my code but the problem still presists but now clicking on a marker focuses on the last marker not just displays the last markers text.
Here is a sample url http://map.projects.webskii./default.html
Share Improve this question edited Jan 28, 2018 at 15:37 Salman Arshad 272k84 gold badges443 silver badges534 bronze badges asked Jun 12, 2013 at 11:45 James Andrew SmithJames Andrew Smith 1,5782 gold badges23 silver badges43 bronze badges3 Answers
Reset to default 14The workaround I use in these situations is to assign an id to the marker upon creation. This id will be available in callbacks so you can use it to fetch the HTML content on demand. Here is how:
var locations = [
[52.478899, -1.894055, 'test 1', '87', 'Name: test 1'],
[52.479474, -1.895946, 'test 2', '88', 'Name: test 2'],
[52.477082, -1.893929, 'test 3', '89', 'Name: test 3']
];
var map = new google.maps.Map(document.getElementById("map_div"), {
center: new google.maps.LatLng(52.528680, -1.839480),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infoWindow = new google.maps.InfoWindow(); // 2: infoWindow is global
var markerClickHandler = function () {
infoWindow.setContent(locations[this.dataId][4]); // 3: use dataId stored in marker to fetch content from locations array
infoWindow.open(map, this);
};
var bounds = new google.maps.LatLngBounds();
var i;
for (i = 0; i < locations.length; i++) {
var latlng = new google.maps.LatLng(locations[i][0], locations[i][1]);
var marker = new google.maps.Marker({
position: latlng,
map: map,
title: locations[i][2],
dataId: i // 1: assign an id
});
bounds.extend(latlng);
google.maps.event.addListener(marker, "click", markerClickHandler);
}
map.fitBounds(bounds);
Demo here. Note that it is also possible to store the Text/HTML into the marker instead of the id.
I guess it always show data for the last record? eg infowindow.setContent(curLoc[4]); curLoc is always the last from dataset locations.
Try this instead
var beachMarker = new google.maps.Marker({
position: myLatLng,
map: map,
title: curLoc[2],
data : curLoc[4], //add content to the Marker-object
icon: image,
zIndex: 50 - i,
childMinderId: curLoc[3]
});
...
google.maps.event.addListener(beachMarker, 'click', (function () {
infowindow.setContent(beachMarker.data);
infowindow.setPosition(beachMarker.position);
infowindow.open(map);
}
Just an addendum to davidkonrad response. Had the same issue with last data popping up. If you use the data on the marker and use 'this' , it picks up the data on the marker and it's position for the marker object.
google.maps.event.addListener(beachMarker, 'click', (function () {
infowindow.setContent(this.data);
infowindow.open(map,this);
}
本文标签: javascriptGoogle maps info windows displaying same informationStack Overflow
版权声明:本文标题:javascript - Google maps info windows displaying same information - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741850299a2401026.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论