Google Map未显示检索到的地址中的所有标记

时间:2013-07-18 11:07:01

标签: google-maps google-maps-api-3 google-maps-markers

美好的一天,

我正在尝试在我的谷歌地图上添加标记,但并非所有标记显示。只显示第一个地址标记。请参阅以下代码:

//Search Button Event Handler
function getAccounts() {

    //Search where City contains data
    var Search = SearchText.value;
    //alert("You have inserted: " + Search + " as a city");
    if (Search != "") {
        retrieveMultiple("AccountSet", "substringof('" + Search + "',Address1_City)", SearchCompleted, null);
    }

    //Retrieve all Accounts
    else {
        retrieveMultiple("AccountSet", null, SearchCompleted, null);
    }
}

//Callback - Search Success
function SearchCompleted(data, textStatus, XmlHttpRequest) {

    if (data && data.length > 0) {  
        for (var i=0; i < data.length; i++) {                
            var result = data[i];
            var address = (data[i].Address1_Line1 + ", " + data[i].Address1_City + ", " + data[i].Address1_StateOrProvince + ", " + data[i].Address1_Country);

            showAddress(address);

            alert(result.Address1_Line1 + ", " +result.Address1_City + ", " + result.Address1_StateOrProvince + ", " + result.Address1_Country);
        }
    }
    else {
        alert('No records!');
    }
}


function showAddress(address) {
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({ 'address': address}, function (result, status) {
        if (status == google.maps.GeocoderStatus.OK) {

            var myOptions = {
                zoom: 3,
                center: result[0].geometry.location,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            var map = new google.maps.Map(document.getElementById("map"), myOptions);
            //map.setCenter(result[0].geometry.location);
            var marker = new google.maps.Marker({
                map: map,
                position: result[0].geometry.location
            });
        } else {
            Alert("Geocode was not successful for the following reason: "+ Status); // address not found
        }
    });

}

// ]]>

我知道信息被正确检索,因为alert()显示了所有检索到的信息

1 个答案:

答案 0 :(得分:1)

您的showAddress函数为每个标记创建一个新地图。如果要显示多个,则需要创建一个地图并将所有标记添加到其中。改变这个:

function showAddress(address) {
  var geocoder = new google.maps.Geocoder();
  geocoder.geocode({ 'address': address}, function (result, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      // creates a new map object
      var myOptions = {
        zoom: 3,
        center: result[0].geometry.location,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      var map = new google.maps.Map(document.getElementById("map"), myOptions);
      //map.setCenter(result[0].geometry.location);
      var marker = new google.maps.Marker({
        map: map,
        position: result[0].geometry.location
      });
    } else {
      Alert("Geocode was not successful for the following reason: "+ Status); // address not found
    }
  });
}

对此:

function showAddress(address, map) {
  var geocoder = new google.maps.Geocoder();
  geocoder.geocode({ 'address': address}, function (result, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      var marker = new google.maps.Marker({
        map: map,
        position: result[0].geometry.location
      });
    } else {
      Alert("Geocode was not successful for the following reason: "+ Status); // address not found
    }
  });
}

在调用之前创建地图(在您发布的代码之外的某个地方)。