2013-08-06 82 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调用是异步的,您需要使用回调例程中的数据,它可用。

相关问题