如何使用坐标数组获取最大/最小边界 [英] How to get maximum/minimum boundaries with array of coordinates

查看:117
本文介绍了如何使用坐标数组获取最大/最小边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以通过一组coodinates来帮助我理解如何获得最大/最小坐标的逻辑吗?我想要获得的是它从这些坐标数组中可以获得的最长距离。例如。

Can anyone help me with the logic on how should I get the maximum/minimum coordinates, with an array of coodinates? What I'm trying to get is the longest distance it can get out of those array of coordinates. Ex.


    var coordinates = [{
       lat: -231,
       lng: 223l
    }, {
       lat: 43,
       lng: -4323
    }, {
       lat: 42312,
       lng: -231
    }, {
       lat: 435345,
       lng: -6563
    }]

    // some filter calculation here...


    // This is what I need
    var min_coords = { lat, lng }
    var max_coords = { lat, lng }


推荐答案

您可以创建值的数组,然后使用 Math.max Math.min 来获取最大值和最小值

You can create arrays of the values, and then use Math.max and Math.min to get the highest and lowest values

var coordinates = [{
    lat: -231,
    lng: 223
}, {
    lat: 43,
    lng: -4323
}, {
    lat: 42312,
    lng: -231
}, {
    lat: 435345,
    lng: -6563
}]

var lat = coordinates.map(function(p) {return p.lat});
var lng = coordinates.map(function(p) {return p.lng});

var min_coords = {
    lat : Math.min.apply(null, lat),
    lng : Math.min.apply(null, lng)
}
var max_coords = {
    lat : Math.max.apply(null, lat),
    lng : Math.max.apply(null, lng)
}

console.log(min_coords);
console.log(max_coords);

这篇关于如何使用坐标数组获取最大/最小边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆