向MapQuest JavaScript地理代码模块动态添加地址
我试图在JavaScript API中使用MapQuest地理编码模块来动态批处理地理编码地址:向MapQuest JavaScript地理代码模块动态添加地址,javascript,geocoding,mapquest,Javascript,Geocoding,Mapquest,我试图在JavaScript API中使用MapQuest地理编码模块来动态批处理地理编码地址: MQA.withModule('geocoder', function() { var addresses = new Array(); $.getJSON('get_marker_json.php', function( data ) { $.each(data, function(i, item) { addresses.push(s
MQA.withModule('geocoder', function() {
var addresses = new Array();
$.getJSON('get_marker_json.php', function( data ) {
$.each(data, function(i, item) {
addresses.push(street: item.address, city: item.city, state: item.state, postalCode: item.zip);
});
});
/*Pass an array of locations to be geocoded and placed on map*/
map.geocodeAndAddLocations(
// add addresses from array here
addresses
);
});
然而,这不起作用。似乎地址必须是预定义的。例如:
map.geocodeAndAddLocations([
'Littleton CO',
{ city: 'Steamboat Springs', state: 'CO' },
{ street: '555 17th St', postalCode: '80202' },
'Winter Park CO'
]);
我怎样才能做到这一点
谢谢 调用getJSON()
是异步的,这意味着map.geocodeandddlocations()
正在运行,而网络请求仍在执行,并且地址
为空
组装
地址
数组后,将对map.geocodeandddlocations()的调用移动到getJSON()中的回调中。(另外,为什么要组装JSON字符串数组?您还可以通过预先添加以下内容使getJSON()调用同步,并使其等待数组填充完成:
$.ajaxSetup({'async': false});
谢谢!你说得对,我将上面的代码更正为一个对象数组。