在提供的数组之间获取所有经纬度对的好方法是什么?
提供的纬度和经度的示例数组:
//HTML
<Button text="Show/hide block" (tap)="onTap()" class="btn btn-primary btn-active"></Button>
<GridLayout *ngIf="isVisible" class="bg-primary" borderRadius="2" height="300"></GridLayout>
//Component
...
onTap() {
if (this.isVisible) {
this.isVisible = false;
} else {
this.isVisible = true;
}
}
...
理想情况下,我然后将var locs = [{
lat: -96.7260,
lng: 44.2599
},
{
lat: -96.7315,
lng: 44.3334
},
{
lat: -96.8434,
lng: 44.3321
},
{
lat: -96.8551,
lng: 44.2525
}
]
function getLowAndHighValues(locArr) {
var latLow = Number.POSITIVE_INFINITY,
latHigh = Number.NEGATIVE_INFINITY,
lngLow = Number.POSITIVE_INFINITY,
lngHigh = Number.NEGATIVE_INFINITY;
var tmpLng, tmpLat;
for (var i = locArr.length - 1; i >= 0; i--) {
tmpLng = locArr[i].lng;
tmpLat = locArr[i].lat;
if (tmpLng < lngLow) lngLow = tmpLng;
if (tmpLng > lngHigh) lngHigh = tmpLng;
if (tmpLat < latLow) latLow = tmpLat;
if (tmpLat > latHigh) latHigh = tmpLat;
}
return {
latLow,
latHigh,
lngLow,
lngHigh
}
}
var lowHighVals = getLowAndHighValues(locs); // returns {latLow: -96.8551, latHigh: -96.726, lngLow: 44.2525, lngHigh: 44.3334}
传递给lowHighVals
,以了解如何增加值:
getPairValuesBetweenLowAndHigh
function getPairValuesBetweenLowAndHigh(lowHighVals, inc) {
var { latLow, latHigh, lngLow, lngHigh } = lowHighVals;
var pairs = [];
var tempLoc = { lat: latLow, lng: lngLow }
// where I am stuck
return pairs;
}
getPairValuesBetweenLowAndHigh(lowHighVals, 0.01);
会返回以下内容:
getPairValuesBetweenLowAndHigh(lowHighVals, 0.01)
答案 0 :(得分:0)
var locs = [{
lat: -96.7260,
lng: 44.2599
},
{
lat: -96.7315,
lng: 44.3334
},
{
lat: -96.8434,
lng: 44.3321
},
{
lat: -96.8551,
lng: 44.2525
}
];
var smallestLat = locs[0], smallestLng = locs[0], biggestLat = locs[0], biggestLng = locs[0];
locs.forEach(function(coord){
if(coord.lat < smallestLat.lat) smallestLat = coord;
if(coord.lng < smallestLng.lng) smallestLng = coord;
if(coord.lat > biggestLat.lat) biggestLat = coord;
if(coord.lng > biggestLng.lng) biggestLng = coord;
});
console.log("smallestLat",smallestLat)
console.log("smallestLng",smallestLng)
console.log("biggestLat",biggestLat)
console.log("biggestLng",biggestLng)
result = {
highest : {
lat: biggestLat.lat > biggestLng.lat?biggestLat.lat:biggestLng.lat,
lng: biggestLat.lng > biggestLng.lng?biggestLat.lng:biggestLng.lng,
},
lowest: {
lat: smallestLat.lat > smallestLng.lat?smallestLat.lat:smallestLng.lat,
lng: smallestLat.lng > smallestLng.lng?smallestLat.lng:smallestLng.lng,
}
}
console.log("result", result);
答案 1 :(得分:0)
对此。我对如何对其进行优化很感兴趣。
var locs = [{
lat: 44.2599,
lng: -96.7260
},
{
lat: 44.3334,
lng: -96.7315
},
{
lat: 44.3321,
lng: -96.8434
},
{
lat: 44.2525,
lng: -96.8551
}
]
function getLowAndHighValues(locArr) {
var latLow = Number.POSITIVE_INFINITY,
latHigh = Number.NEGATIVE_INFINITY,
lngLow = Number.POSITIVE_INFINITY,
lngHigh = Number.NEGATIVE_INFINITY;
var tmpLng, tmpLat;
for (var i = locArr.length - 1; i >= 0; i--) {
tmpLng = locArr[i].lng;
tmpLat = locArr[i].lat;
if (tmpLng < lngLow) lngLow = tmpLng;
if (tmpLng > lngHigh) lngHigh = tmpLng;
if (tmpLat < latLow) latLow = tmpLat;
if (tmpLat > latHigh) latHigh = tmpLat;
}
return {
latLow,
latHigh,
lngLow,
lngHigh
}
}
function getPairValuesBetweenLowAndHigh(lowHighVals, precision, fixed) {
var {
latLow,
latHigh,
lngLow,
lngHigh
} = lowHighVals;
var pairs = [],
tempLoc;
var latDiff = parseFloat((Math.abs(latLow - latHigh) / precision).toFixed(8));
var lngDiff = parseFloat((Math.abs(lngLow - lngHigh) / precision).toFixed(8));
// push pairs to points
for (var i = latLow; i <= latHigh; i += latDiff) {
tempLoc = {
lat: parseFloat(i.toFixed(fixed)),
lng: lngLow
}
pairs.push(tempLoc);
if ((lngLow === lngHigh) && parseFloat(i.toFixed(fixed)) === latHigh) break;
if (parseFloat(i.toFixed(fixed)) === latHigh) {
i = latLow;
lngLow += lngDiff
}
}
// remove duplicates
pairs = pairs.filter((pair, index) => {
return index === pairs.findIndex(obj => {
return JSON.stringify(obj) === JSON.stringify(pair);
});
});
return pairs;
}
var lowHighVals = getLowAndHighValues(locs);
var testResponse = getPairValuesBetweenLowAndHigh(lowHighVals, 8, 4);
console.log("Response", testResponse);