Google 地图对地址数组进行地理编码无法正常工作 - 只有一个标记
Google Maps geocoding an array on address not working properly - only one marker
我正在尝试制作一张带有多个标记的地图。
如果我用坐标来做,效果很好。问题是我没有所有地方的坐标。我当然知道的是地址。
所以我选择了地理编码。它只用一个标记就可以正常工作,但我不明白为什么,它不会循环遍历位置数组。
这是我的代码:
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));
}
您可以看到按预期工作的经纬度版本的注释掉的行。
将依赖于标记的代码放在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);
html,
body,
#mappa {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="mappa"></div>
我正在尝试制作一张带有多个标记的地图。
如果我用坐标来做,效果很好。问题是我没有所有地方的坐标。我当然知道的是地址。
所以我选择了地理编码。它只用一个标记就可以正常工作,但我不明白为什么,它不会循环遍历位置数组。
这是我的代码:
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));
}
您可以看到按预期工作的经纬度版本的注释掉的行。
将依赖于标记的代码放在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);
html,
body,
#mappa {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="mappa"></div>