简体   繁体   English

如何从一个笛卡尔坐标获得边界框?

[英]How to get a bounding box from one cartesian coordinate?

I'm trying to get a bounding box coordinates in javascript from just 1 coordinate (latitude,longitude), then I could use it as viewbox in the OSM Nominatin API . 我试图从1坐标(纬度,经度)获得javascript中的边界框坐标,然后我可以将其用作OSM Nominatin API中的视图框。

For the red point, how to get the green points for any area/country in the planet ? 对于红点,如何获得地球上任何地区/国家的绿点? (Raw difference of 0.15 for example): (原始差异为0.15):

在此输入图像描述

for (x,y) will be: (x-2,y+2), (x+2,y-2)?


function get_bounce(lat, lng) { // y,x
    // x-0.15,y+0.15
    //          x,y
    //               x+0.15,y-0.15
    var lng1 = lng - 0.15;
    var lat1 = lat + 0.15;
    var lng2 = lng + 0.15;
    var lat2 = lat - 0.15;
    return [lat1,lng1,lat2,lng2];
}

Thanks in advance! 提前致谢!

The 1 of degree longitude distance varies as was said in a comment depending on the latitude you are at. 经度距离的1度会根据您所在的纬度而在评论中说明。 Longitude is wider at the equator and narrows down at the poles, unlike latitude that has a constant size across the globe. 赤道的经度较宽,而极地的经度则较窄,不像纬度在全球范围内具有恒定的大小。 To visualize this see: 要想象这个看到:

Visualize the globe 可视化地球

Another good reference that helped me was: 帮助我的另一个好参考是:

Calculate distance, bearing and more between Latitude/Longitude points 计算纬度/经度点之间的距离,方位和更多

I had a similar problem. 我遇到了类似的问题。 Here is the code I came up with to get a pretty good bounding box. 这是我想出的代码,以获得一个非常好的边界框。 Give this function a gps location string ("x.xx, y.yy") and "radius" in km and it generates the north, south, east, west decimal coordinates for a bounding box. 为此函数提供gps位置字符串(“x.xx,y.yy”)和“radius”(以km为单位),它为边界框生成北,南,东,西十进制坐标。 This is in Java but should be easy to convert to another language. 这是Java,但应该很容易转换为另一种语言。

    // Compute bounding Box coordinates for use with Geonames API.
    class BoundingBox
    {
        public double north, south, east, west;
        public BoundingBox(String location, float km)
        {
            //System.out.println(location + " : "+ km);
            String[] parts = location.replaceAll("\\s","").split(","); //remove spaces and split on ,

            double lat = Double.parseDouble(parts[0]);
            double lng = Double.parseDouble(parts[1]);

            double adjust = .008983112; // 1km in degrees at equator.
            //adjust = 0.008983152770714983; // 1km in degrees at equator.

            //System.out.println("deg: "+(1.0/40075.017)*360.0);


            north = lat + ( km * adjust);
            south = lat - ( km * adjust);

            double lngRatio = 1/Math.cos(Math.toRadians(lat)); //ratio for lng size
            //System.out.println("lngRatio: "+lngRatio);

            east = lng + (km * adjust) * lngRatio;
            west = lng - (km * adjust) * lngRatio;
        }

    }

The above code give a pretty good approximation that can be off a mile or two depending on your latitude. 上面的代码提供了一个非常好的近似值,根据您的纬度,可能会偏离一两英里。 If you want to be more accurate then you might take a look at the answers here: 如果您想要更准确,那么您可以在这里查看答案:

Geotools: bounding box for a buffer in wgs84 See Eduardo's answer to this question. Geotools:wgs84中缓冲区的边界框请参阅Eduardo对此问题的回答。 It seems like a pretty accurate solution. 这似乎是一个非常准确的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM