【发布时间】:2016-03-19 06:34:44
【问题描述】:
我对 jQuery 或 Javascript 有疑问。我正在尝试在 IP 数组中的 Google 地图中显示更多标志。我设法将 IP 数组传递给函数,但是当我使用 ajax 调用 web api 的次数与数组长度一样多时出现问题,结果 locations 数组为空(未定义);
这是我的代码
function initialize(ipList)
{
var locations = [];
var ips = ipList;
var apiUrl = 'http://freegeoip.net/json/';
// for(var i = 0; i < ips.length; i++)
// {
// jQuery.ajax
// ({
// url: apiUrl + ips[i],
// type: 'POST',
// dataType: 'jsonp',
// success: function(location)
// {
// if(location != null)
// {
// locations.push(location);;
// }
// }
// });
// }
$.each(ips, function(i, x)
{
$.ajax
({
url: apiUrl + x,
type: "POST",
cache: false,
success: function(data)
{
if(data != null)
{
locations[i] = location;
}
}
});
});
var properties =
{
center: new google.maps.LatLng(locations[0].latitude, locations[0].longitude),
zoom: 10,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map"),properties);
for (var i = 0; i < locations.length; i++)
{
var marker = new google.maps.Marker
({
position:{lat: locations[i].latitude, lng: locations[i].longitude},
animation:google.maps.Animation.BOUNCE,
title: locations[i].city
});
marker.setMap(map);
}
}
【问题讨论】:
标签: javascript arrays ajax asp.net-web-api