如何在JavaScript数组中从数据库中提取所有元素?
我需要在地图上放置谷歌地图标记,下面是我想用于此任务的代码:如何在JavaScript数组中从数据库中提取所有元素?,javascript,google-maps,google-maps-api-3,google-maps-markers,Javascript,Google Maps,Google Maps Api 3,Google Maps Markers,我需要在地图上放置谷歌地图标记,下面是我想用于此任务的代码: function codeAddress() { var address = document.getElementById('address').value; geocoder.geocode( { 'address': address}, function(results, status) { if (status == google.maps.GeocoderStatus.OK) {
function codeAddress() {
var address = document.getElementById('address').value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
现在这个脚本可以工作了,但我想修改它以用于地址数组。你能告诉我怎么做吗?我将从php SQL循环中获取地址。我将修改您的函数以接受地址作为参数:
function codeAddress(address) {
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
然后,当您获得数组数据时,在其上循环并调用您的函数
var addresses; // array of address
for (index = 0; index < addresses.length; ++index) {
codeAddress(addresses[index]);
}
var地址;//地址数组
对于(索引=0;索引
如果您的数组大于约10,您将遇到配额/速率限制;比这大得多,最好使用坐标而不是地址(对地址进行地理编码并存储坐标)。