Javascript 从AJAX调用填充数组并将其返回给调用函数
我正在为Javascript 从AJAX调用填充数组并将其返回给调用函数,javascript,jquery,html,google-maps,google-maps-api-3,Javascript,Jquery,Html,Google Maps,Google Maps Api 3,我正在为googlemapsapi和jQuery使用gmap3扩展 我有一个.gpx文件,我通过AJAX读取该文件,获取每个坐标并将其添加到数组中。然后返回此数组以填充地图中的多段线 在函数测试中,我会在返回它之前进行点计数,计数为0 下面是我处理.gpx文件以返回坐标的函数: function Test() { var points = []; $.ajax({ type: "GET", url: "gpx/12345.gpx",
googlemapsapi
和jQuery
使用gmap3
扩展
我有一个.gpx
文件,我通过AJAX读取该文件,获取每个坐标并将其添加到数组中。然后返回此数组以填充地图中的多段线
在函数测试中,我会在返回它之前进行点计数,计数为0
下面是我处理.gpx文件以返回坐标的函数:
function Test() {
var points = [];
$.ajax({
type: "GET",
url: "gpx/12345.gpx",
dataType: "xml",
success: function (xml) {
$(xml).find("trkpt").each(function () {
var lat = $(this).attr("lat");
var lon = $(this).attr("lon");
var p = new google.maps.LatLng(lat, lon);
points.push(p);
});
}
});
return points;
}
$(document).ready(function () {
var points = Test();
var route1Latlng = new google.maps.LatLng(-33.7610590, 18.9616790);
$('#map_canvas').gmap3({
map: {
options: {
center: route1Latlng,
zoom: 11
}
},
polyline: {
options: {
path: points,
strokeColor: '#FF00AA',
strokeOpacity: .7,
strokeWeight: 4
}
}
});
});
在IE9中没有绘制多段线,但在FireFox中绘制正确。发生的事情是调用test返回点,因为这是一个异步请求,它不会拖拽程序的执行,而是返回空的点数组。您正在使用空数组进行绘图,因此您的线未显示。它只会在ajax请求完成后显示。因此,我们将标记的绘制包装到另一个函数中,并在ajax调用完成后调用它 尝试在ajax成功事件中调用函数,如下所示
function Test() {
var points = [];
$.ajax({
type: "GET",
url: "gpx/12345.gpx",
dataType: "xml",
success: function (xml) {
$(xml).find("trkpt").each(function () {
var lat = $(this).attr("lat");
var lon = $(this).attr("lon");
var p = new google.maps.LatLng(lat, lon);
points.push(p);
});
drawPolyline(points); //when all the points have been loaded then we call this method
}
});
}
function drawPolyline(pointsTobeDrawn){ // this method gets the points and plots it
var route1Latlng = new google.maps.LatLng(-33.7610590, 18.9616790);
$('#map_canvas').gmap3({
map: {
options: {
center: route1Latlng,
zoom: 11
}
},
polyline: {
options: {
path: pointsTobeDrawn,
strokeColor: '#FF00AA',
strokeOpacity: .7,
strokeWeight: 4
}
}
});
}
$(document).ready(function () {
Test(); // we need only this method as it will plot the markers on success event of it
});
使用ajax false作为
$.ajax({
async: false, //dont use quotes here like "false".
type: "GET",
url: "gpx/12345.gpx",
dataType: "xml",
success: function (xml) {
$(xml).find("trkpt").each(function () {
var lat = $(this).attr("lat");
var lon = $(this).attr("lon");
var p = new google.maps.LatLng(lat, lon);
points.push(p);
});
drawPolyline(points);
}
});
如果我们将async设置为false,那么我们的程序将等待ajax请求完成它的过程
阅读有关ajax调用工作原理和线程的更多说明调用test返回点时发生了什么,因为这是一个异步请求,它不会拖拽程序的执行,而是返回空的点数组。您正在使用空数组进行绘图,因此您的线未显示。它只会在ajax请求完成后显示。因此,我们将标记的绘制包装到另一个函数中,并在ajax调用完成后调用它 尝试在ajax成功事件中调用函数,如下所示
function Test() {
var points = [];
$.ajax({
type: "GET",
url: "gpx/12345.gpx",
dataType: "xml",
success: function (xml) {
$(xml).find("trkpt").each(function () {
var lat = $(this).attr("lat");
var lon = $(this).attr("lon");
var p = new google.maps.LatLng(lat, lon);
points.push(p);
});
drawPolyline(points); //when all the points have been loaded then we call this method
}
});
}
function drawPolyline(pointsTobeDrawn){ // this method gets the points and plots it
var route1Latlng = new google.maps.LatLng(-33.7610590, 18.9616790);
$('#map_canvas').gmap3({
map: {
options: {
center: route1Latlng,
zoom: 11
}
},
polyline: {
options: {
path: pointsTobeDrawn,
strokeColor: '#FF00AA',
strokeOpacity: .7,
strokeWeight: 4
}
}
});
}
$(document).ready(function () {
Test(); // we need only this method as it will plot the markers on success event of it
});
使用ajax false作为
$.ajax({
async: false, //dont use quotes here like "false".
type: "GET",
url: "gpx/12345.gpx",
dataType: "xml",
success: function (xml) {
$(xml).find("trkpt").each(function () {
var lat = $(this).attr("lat");
var lon = $(this).attr("lon");
var p = new google.maps.LatLng(lat, lon);
points.push(p);
});
drawPolyline(points);
}
});
如果我们将async设置为false,那么我们的程序将等待ajax请求完成它的过程
阅读有关ajax调用的工作原理和线程的更多说明您必须先等待ajax调用才能获取指针。如果是本地文件,那么应该很快。IE不显示它,FF显示它。你必须先等待ajax调用才能获取点。如果是本地文件,应该很快。IE不显示,FF显示。你能解释一下你的答案吗?更新了答案。如果您需要更多的澄清,请告诉我。您能解释一下您的答案吗?更新了答案。如果你需要更多的澄清,请告诉我