我知道,這是一個經常性的,但...我有一個很大的問題,我的多個標記:他們都有相同的標題和相同的infowindow內容。GMaps API V3 - 多標記和信息窗口
我在很多網站上搜索過,但我沒有找到我的代碼中的問題在哪裏。 我希望你能幫助我
這是我的代碼(我相信這是如此!):
<script type="text/javascript">
var map;
var geocoder;
$(document).ready(function() {
initializeMap();
});
function initializeMap() {
var people = [{"userid":"47","lastname":"lastname1","firstname":"firstname1","address":"SomeAddress","phone1":"00000000000","phone2":"","email":"[email protected]"},{"userid":"42","lastname":"lastname2","firstname":"firstname2","address":"SomeAddress2","phone1":"0","phone2":"0","email":"[email protected]"}];
var center = new google.maps.LatLng(40.667, -73.833);
var myOptions = {
zoom: 8,
center: center,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
geocoder = new google.maps.Geocoder();
for (i = 0; i < people.length; i++) {
var p = people[i];
geocoder.geocode({ 'address': p["address"]}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map:map,
draggable:false,
animation: google.maps.Animation.DROP,
title:p["lastname"] + " " + p["firstname"],
position: results[0].geometry.location
});
var myWindowOptions = {
content:
'<h3>'+p["lastname"] + " " + p["firstname"]+'</h3>'+
'<p>'+p["address"]
};
var myInfoWindow = new google.maps.InfoWindow(myWindowOptions);
google.maps.event.addListener(marker, 'click', function() {
myInfoWindow.open(map,marker);
});
});
}
}
</script>
我真的希望有人能告訴我我的錯誤(S)和非常感謝您的幫助!
不,我真的不明白, 這應該是我沒有在我的代碼中看到:
<script type="text/javascript">
var map;
var geocoder;
$(document).ready(function() {
initializeMap();
});
function initializeMap() {
var people = [{"userid":"47","lastname":"lastname1","firstname":"firstname1","address":"SomeAddress","phone1":"00000000000","phone2":"","email":"[email protected]"},{"userid":"42","lastname":"lastname2","firstname":"firstname2","address":"SomeAddress2","phone1":"0","phone2":"0","email":"[email protected]"}];
var center = new google.maps.LatLng(50.833, 25.000);
var myOptions = {
zoom: 8,
center: center,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
geocoder = new google.maps.Geocoder();
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < people.length; i++) {
alert(people[i]['address']);
geocoder.geocode({ 'address': people[i]["address"]}, function(results, status) {
marker = new google.maps.Marker({
position: results[0].geometry.location,
map: map
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent('<h3>'+people[i]["lastname"] + " " + people[i]["firstname"]+'</h3>');
infowindow.open(map, marker);
}
})(marker, i));
});
}
}
</script>
感謝您的幫助! :)