如何从 JavaScript 数组中提取数据库中的所有元素?

How to pull in all elements from database in a JavaScript array?

我需要在地图上放置 google 地图标记,这是我想用于此任务的代码:

  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]);
}