2013-08-06 87 views
0

我試圖在我的Google地圖上通過XML文件中的城市和國家名稱實現標記。不幸的是,我似乎無法獲得保存國家對象的全局數組(國家列表),以保持在XML解析器之外。在解析器中使用警報,我可以看到它有35個項目,但外部有0個項目。我正在尋找一些見解,但我認爲它與這些函數的加載順序有關。由於陣列沒有放置任何物品,因此沒有城市可以填充地圖標記。XML解析器和Google地圖地理編碼 - 陣列不會填充

<script type="text/javascript"> 


$(document).ready(function() { 
    $.ajax({ 
     type: "GET", 
     url: "countries.xml", 
     dataType: "xml", 
     success: parseXML 
    }); 
}); 
var countryList = []; 
    function parseXML(xml) { 
     $(xml).find("country").each(function() { 
      var name = $(this).find("name").text(); 
      var capital = $(this).find("capital").text(); 


      countryList.push(new CountryObject(name, capital)); 

     }); 
     //alert(countryList.length);  this shows a count of 35`enter code here` 
} 

//alert(countryList.length); this shows a count of 0 

function CountryObject(name, capital) { 
    this.name = name; 
    this.capital = capital; 
} 


var geocoder; 
var map; 
var markersArray = []; 
var bounds; 
var infowindow = new google.maps.InfoWindow({ 
    content: '' 
}); 

//plot initial point using geocode instead of coordinates (works just fine) 
function initialize() { 

    geocoder = new google.maps.Geocoder(); 
    bounds = new google.maps.LatLngBounds(); 

    var myOptions = { 
     zoom: 4, 
     mapTypeId: google.maps.MapTypeId.ROADMAP, 
     navigationControlOptions: { 
      style: google.maps.NavigationControlStyle.SMALL 
     } 
    }; 
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 

    geocoder.geocode({ 'address': 'Toronto Canada'}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      map.setCenter(results[0].geometry.location); 

      marker = new google.maps.Marker({ 
       map: map, 
       position: results[0].geometry.location 
      }); 

      bounds.extend(results[0].geometry.location); 

      markersArray.push(marker); 
     } 
     else{ 
      alert("Geocode was not successful for the following reason: " + status); 
     } 
    }); 
    plotMarkers(); 
} 

var locationsArray = []; 

for (var j = 0; j < countryList.Length; j++) { 
    var loc = countryList[j].capital + ' ' + countryList[j].name; 
     locationsArray[j] = [countryList[j].capital, loc]; 
} 


function plotMarkers(){ 
    var i; 
    for(i = 0; i < locationsArray.length; i++){ 
     codeAddresses(locationsArray[i]); 
    } 
} 

function codeAddresses(address){ 
    geocoder.geocode({ 'address': address[1]}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      marker = new google.maps.Marker({ 
       map: map, 
       position: results[0].geometry.location 
      }); 

      google.maps.event.addListener(marker, 'click', function() { 
       infowindow.setContent(address[0]); 
       infowindow.open(map, this); 
      }); 

      bounds.extend(results[0].geometry.location); 

      markersArray.push(marker); 
     } 
     else{ 
      alert("Geocode was not successful for the following reason: " + status); 
     } 

     map.fitBounds(bounds); 
    }); 
} 

google.maps.event.addDomListener(window, 'load', initialize); 
</script> 
</head> 

<body onload="initialize()"> 

編輯:我正在尋找利用收集的countryList陣列中的信息來填充使用locationsArray標記(而不是硬編碼值,其中大部分的例子在線使用)。但是,我不完全理解我缺少的內容以填充countryList,除了我在SO上找到的內容外,我不太熟悉同步和異步。

+0

你能更好地定義你的問題嗎?編輯帖子並在最後添加一個問題,以幫助他人給你一個很好的答案。 – Gidil

+0

你的countries.xml文件有多長?即時對所有這些地址進行地理編碼的風險都會遇到Geocoder配額和/或速率限制。您可以在頁面加載時可靠地對大約10個位置進行可靠的地理編碼,而不會觸及查詢限制,但這取決於Google服務器上的負載。您應該在XML中包含座標。 – geocodezip

回答

0

ajax調用是異步的,您需要使用回調例程中的數據,它可用。

相關問題