我在PHP文件中声明了一些全局变量:
var coordinates = [];
var joined_data = [];
变量坐标最初设置为从XMLHttpRequest接收的一些数据, joined_data 设置为等于坐标。 稍后,调用一个函数:
function addMarkerToWps(wp_id)
{
joined_data = coordinates.slice(0, coordinates.length-5);
calculateRoutes();
}
此简短代码应选择一个子阵列并将其保存到 joined_data ;函数 calculateRoutes 使用Google API在地图上绘制路径;在这样的函数结束时,我尝试显示两个数组的长度,我希望 joined_data 比坐标更短,但我发现它们的大小相同。 所以,我怀疑 slice 函数以某种方式失败了。 这是函数 calculateRoutes :
function calculateRoutes()
{
// remove previous route
removeRoutes();
//window.alert('Numero punti ' + coordinates.length);
// set start position
//var begin = new google.maps.LatLng(coordinates[0].Lat, coordinates[0].Lon);
var begin = new google.maps.LatLng(joined_data[0].Lat, joined_data[0].Lon);
// set end position
var end = null;
// waypoints struct
var waypoints = [];
// counter
var wCount = 0;
// loop
var i;
//for(i=1; i<coordinates.length-1; i++)
for(i=1; i<joined_data.length-1; i++)
{
if(wCount === REQUEST_WAYPOINTS_LIMIT)
{
// increment counter to get end's index
i++;
// set end
//end = new google.maps.LatLng(coordinates[i].Lat, coordinates[i].Lon);
end = new google.maps.LatLng(joined_data[i].Lat, joined_data[i].Lon);
// draw current route
drawRoute(begin, end, waypoints);
// update indeces
begin = end;
// reset structures
wCount = 0;
waypoints = [];
}
// add waypoint to list
waypoints.push
(
{
//location: new google.maps.LatLng(coordinates[i].Lat, coordinates[i].Lon),
location: new google.maps.LatLng(joined_data[i].Lat, joined_data[i].Lon),
stopover: true
}
);
// increments counter
wCount++;
}
if(waypoints.length > 0)
{
//end = new google.maps.LatLng(coordinates[coordinates.length-1].Lat, coordinates[coordinates.length-1].Lon);
end = new google.maps.LatLng(joined_data[joined_data.length-1].Lat, joined_data[joined_data.length-1].Lon);
drawRoute(begin, end, waypoints);
}
for(var i = 0; i<coordinates.length; i++)
{
placeMarker(coordinates[i].Lat, coordinates[i].Lon, "http://gmaps-samples.googlecode.com/svn/trunk/markers/red/blank.png");
}
for(var j = 0; j<interest_points.length; j++)
{
placeAttractionMarker(
interest_points[j].Lat,
interest_points[j].Lon,
"http://gmaps-samples.googlecode.com/svn/trunk/markers/orange/blank.png",
interest_points[j].Name,
interest_points[j].Wikipedia,
j
);
}
//joinData();
//window.alert("" + coordinates.length + " " + interest_points.length + " " + joined_data.length);
window.alert("" + coordinates.length + " " + joined_data.length);
}
你能发现错误吗?谢谢。