此 JavaScript 数组包含我发送到 Google Maps API 以在 map 中绘制路径的坐标。 有时坐标[0]为空,因为它取决于之前可能存在或不存在的路线(使用 Google map )。
这是我的代码:
coordinates = [];
(在此处之间,我查找先前的路径(坐标[0])。在本例中,我们假设它为空。所以我们有这些值:
coordinates[1] = '50,20'; //route 1
coordinates[2] = '10,18'; //route 2
coordinates[3] = '27,34'; //route 3
之后,我在 map 上绘制路径,如下所示:
travel_path = new google.maps.Polyline({
path: coordinates
});
travel_path.setMap(map);
问题:当没有坐标[0](当没有行进路径的前一段时发生)时,.Polyline 方法会抛出错误,因为它需要接收一个数组从 0 开始的排序索引。
问题:如何将原始数组转换为此(比较索引):
coordinates[0] = '50,20'; //route 1
coordinates[1] = '10,18'; //route 2
coordinates[2] = '27,34'; //route 3
最佳答案
coordinates.shift()
从数组中删除第一个元素。
var coordinates = [];
//coordinates[0] = undefined;
coordinates[1] = '50,20'; //route 1
coordinates[2] = '10,18'; //route 2
coordinates[3] = '27,34'; //route 3
coordinates.shift();
coordinates.shift()
会导致这种情况发生:
coordinates[0] = coordinates[1];
coordinates[1] = coordinates[2];
coordinates[2] = coordinates[3];
//coordinates[3] is removed.
关于jquery - 使用 jQuery 修改数组以获取排序索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7540021/