目前我正在从设备获取坐标(纬度,经度),并将它们存储在数组中,如下所示:
[35.23223,-5.293222]
但是,在几次这些坐标中被重复(也许设备发送了相同的坐标等......
为此,我实现了以下内容:
var uniqueCoords = [Array.from(new Set(coords))];
在每次调用时,都会挖掘现有数组并删除任何重复的坐标。
但是,这会导致严重的问题,特别是当(例如)具有新纬度和旧经度(即[35.23223,-5.319399])时,反之亦然。
在这个特定的例子中,uniqueCoords
将深入研究数组,发现35.23223
是重复的并删除的,它将-5.319399
单独留下,在旅程结束时,它可能会结束:
[35.23223,-5.293222,-5.319399]
我在这里想要的是,只有当两者(经度和经度)与阵列上已经存在的一对完全相同时,才删除(纬度/经度)。
当前代码:
this.storage.get('route').then((route) => {
let uniqueCoords: any = [Array.from(new Set(route))];
uniqueCoords.push(latLng.lat, latLng.lng);
this.storage.set('routeTaken', uniqueCoords);
}).catch((error) => {
this.presentAlert(error)
})
原始数据数组:
[35.7790733,-5.8453983,35.779335,-5.8465283,35.779705,-5.84782,35.7787533,-5.8482083,35.7780167,-5.8491983,35.77782,-5.8504883,35.7774783,-5.8518267,35.776955,-5.852945,35.7765,-5.8541383,35.7761667,-5.855425,-5.8566467,35.77628,-5.8579367,35.7763233,-5.8588633,35.776435,-5.8591367,35.7767667,-5.8594817,35.7776267,-5.8586933,35.7785467,-5.8577233,-5.8585467,35.77949,-5.8597567,35.7797183,-5.86081,35.7805917,-5.8606533,35.7817533,-5.8606867,35.7826217,-5.8618667,35.78295,-5.8636367,35.7834217,-5.8643667]
您可以连接坐标并在唯一化后拆分它们。
var coordinates = [[35.23223, -5.293222], [35.23223, -5.319399], [35.23223, -5.319399]],
unique = Array.from(new Set(coordinates.map(a => a.join('|'))), s => s.split('|').map(Number));
console.log(unique);
.as-console-wrapper { max-height: 100% !important; top: 0; }
如果你取一个点的数组,如[35.23223, -5.293222]
,你就在点中插入一个对象。另一个具有相同坐标的 pont 生成一个新对象,该对象不等于前一个数组。为了使两者相等,您需要对数组进行一些字符串处理。这可以是 JSON 字符串,也可以是更简单的字符串,例如使用分隔符连接。
与单个数组中的连续坐标相同。
var coordinates = [35.23223, -5.293222, 35.23223, -5.319399, 35.23223, -5.319399],
unique = Array.from(new Set(coordinates
.reduce((r, a, i) => (i % 2 ? r[r.length - 1].push(a) : r.push([a]), r), [])
.map(a => a.join('|'))), s => s.split('|').map(Number));
console.log(unique);
.as-console-wrapper { max-height: 100% !important; top: 0; }
这是一个非常合乎逻辑的,一步一步的,没有花哨的狗屎方式
var coords = [
35.7790733, -5.8453983,
35.779335, -5.8465283,
35.7790733, -5.8453983,
35.779705, -5.84782
];
var temp = [];
var unique = [];
var uniqueCoords = [];
for (var i = 0; i < coords.length; i += 2) {
temp.push(coords[i] + '---' + coords[i + 1]); // create some strings
}
for (var i = 0; i < temp.length; i++) {
if (unique.indexOf(temp[i]) === -1) // remove duplicates
unique.push(temp[i]);
}
for (var i = 0; i < unique.length; i++) { // split the strings back into array
uniqueCoords = uniqueCoords.concat(unique[i].split('---'));
}
console.log(uniqueCoords)
如果你只是切换到['lat,lng', ...]
格式(作为字符串),你的代码将正常工作。
但是您应该在添加新坐标后检查重复项。目前你以前做过。
假设uniqueCoords
的格式为[lat,long,lat,long,...]
并且new Set
生成类似于[lat, long]
let coordSet = Array.from(new Set(coords));
// search the uniqueCoords list
let exists = uniqueCoords.find(function (itemCoord, idx, arr) {
// only check on every other coord
// if the previous coord being looped over matches the first coord in the coord set
// if the current coord being looped over matches the second coord in the coord set
// return true indicating the coord-set exists
if ((idx % 2) === 1) && (arr[idx -1] === coordSet[0]) && (itemCoord === coordSet[1]) {
return true;
}
});
// if a matching coord-set wasn't found, push the new set
if (!exists) {
// called with apply so each item in coordSet
// is appended to the uniqueCoords Array
uniqueCoords.push.apply(uniqueCoords, coordSet);
}
this.storage.get('route').then((route) => {
if (route.length < 2 || route[route.length - 2] != latLng.lat || route[route.length - 1] != latLng.lng) {
uniqueCoords.push(latLng.lat, latLng.lng);
this.storage.set('routeTaken', uniqueCoords);
}
}).catch((error) => {
this.presentAlert(error)
})