我正在尝试在其上放置一个包含多个标记的地图。
如果我用坐标做,它可以正常工作。问题是我没有所有地方的坐标。我当然拥有的是地址。
所以我选择了地理编码。它只使用一个标记就可以正常工作,但我不明白为什么,它不会遍历各个位置。
这是我的代码:
var locations = [
['B&B Al Centro Storico', 41.203517, 16.600466, 2,'Via Samuelli 83','Barletta'],
['B&B Cortile Fondo Noce', 41.276672, 16.417772, 3,'Via Fondo Noce 29','Bisceglie']
];
var map = new google.maps.Map(document.getElementById('mappa'), {
zoom: 9,
center: new google.maps.LatLng(41.239162, 16.500301),
mapTypeId: google.maps.MapTypeId.ROADMAP,
scrollwheel: false,
disableDefaultUI: true
});
var geocoder = new google.maps.Geocoder();
var infowindow = new google.maps.InfoWindow();
var marker, i;
var infoWindowContent = [];
function setPin(indirizzo,mappa)
{
geocoder.geocode({'address': indirizzo}, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
marker = new google.maps.Marker({
map: mappa,
position: results[0].geometry.location
});
}
});
}
for (i = 0; i < locations.length; i++) {
// geocoder.geocode({'address': locations[i][4] + ', ' + locations[i][5]}, function(results, status) {
// if (status === google.maps.GeocoderStatus.OK) {
// marker = new google.maps.Marker({
// map: map,
// position: results[0].geometry.location
// });
// }
// });
setPin(locations[i][4] + ' ' + locations[i][5],map);
// marker = new google.maps.Marker({
// position: new google.maps.LatLng(locations[i][1], locations[i][2]),
// map: map
// });
infoWindowContent[i] = '<b>'+locations[i][0]+'</b>';
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(infoWindowContent[i]);
infowindow.open(map, marker);
}
})(marker, i));
}
您可以看到lat的注释掉的行,长版本按预期工作。
答案 0 :(得分:2)
将依赖于标记的代码放在setPin
函数中。
function setPin(indirizzo, i, mappa) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
'address': indirizzo
}, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
marker = new google.maps.Marker({
map: mappa,
position: results[0].geometry.location
});
infoWindowContent[i] = '<b>' + locations[i][0] + '</b>';
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(infoWindowContent[i]);
infowindow.open(mappa, marker);
}
})(marker, i));
} else alert("geocode failed:"+indirizzo+" status="+status)
});
}
代码段
function initialize() {
var locations = [
['B&B Al Centro Storico', 41.203517, 16.600466, 2, 'Via Samuelli 83', 'Barletta'],
['B&B Cortile Fondo Noce', 41.276672, 16.417772, 3, 'Via Fondo Noce 29', 'Bisceglie']
];
var map = new google.maps.Map(document.getElementById('mappa'), {
zoom: 9,
center: new google.maps.LatLng(41.239162, 16.500301),
mapTypeId: google.maps.MapTypeId.ROADMAP,
scrollwheel: false,
disableDefaultUI: true
});
var infowindow = new google.maps.InfoWindow();
var marker, i;
var infoWindowContent = [];
function setPin(indirizzo, i, mappa) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
'address': indirizzo
}, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
marker = new google.maps.Marker({
map: mappa,
position: results[0].geometry.location
});
infoWindowContent[i] = '<b>' + locations[i][0] + '</b>';
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(infoWindowContent[i]);
infowindow.open(mappa, marker);
}
})(marker, i));
} else alert("geocode failed:" + indirizzo + " status=" + status)
});
}
for (i = 0; i < locations.length; i++) {
setPin(locations[i][4] + ' ' + locations[i][5], i, map);
}
}
google.maps.event.addDomListener(window, "load", initialize);
&#13;
html,
body,
#mappa {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
&#13;
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="mappa"></div>
&#13;