gpt4 book ai didi

javascript - 在元组数组中查找边界值的有效方法?

转载 作者:行者123 更新时间:2023-11-30 18:43:22 25 4
gpt4 key购买 nike

我有一组纬度/经度值,就像这样:

var points = ["-0.15868039429188,51.534183502197", "-0.158839,51.534916", "-0.158814,51.53503", "-0.158817,51.535076", "-0.157492,51.535404", "-0.155767,51.535816", "-0.155696,51.535831", "-0.15526,51.535934", "-0.153192,51.536388", "-0.152282,51.536575", "-0.152467,51.536968", "-0.152592,51.53727", "-0.152682,51.53756", "-0.152074,51.53754", "-0.151921,51.537464", "-0.151732,51.538368", "-0.151373,51.538841", "-0.150622,51.539482", "-0.150237,51.539761", "-0.150047,51.539875", "-0.149957,51.539921", "-0.149594,51.540108", "-0.149563,51.540134", "-0.149536,51.540161", "-0.149497,51.540184", "-0.149445,51.540203"];

(好吧,它不是严格意义上的元组数组,但足够接近了。)

我想找到数组的四个边界 - 即纬度和经度北/西/南/东边界。

目前我正在这样做:

                $.each(coords, function(index, value) { 
var j = value.split(',');
var coord_lat = parseFloat(j[1]);
var coord_lng = parseFloat(j[0]);
if (coord_lat>nbound) {
nbound = coord_lat;
} else if (coord_lat<sbound) {
sbound = coord_lat;
}
if (coord_lng<ebound) {
ebound = coord_lng;
} else if (coord_lng>wbound) {
wbound = coord_lng;
}
});

但是,这感觉效率不高。谁能推荐更好的方法?

最佳答案

如果您不受当前输入格式的限制,您可以改用对象。这将避免昂贵的 splitparseFloat 调用。

var points = [
{ latitude: -0.15868039429188, longitude: 51.534183502197 },
{ latitude: -0.158839, longitude: 51.534916 },
{ latitude: -0.155696, longitude: 51.535831 }
];

关于javascript - 在元组数组中查找边界值的有效方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6202751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com