简体   繁体   中英

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 .

For the red point, how to get the green points for any area/country in the planet ? (Raw difference of 0.15 for example):

在此输入图像描述

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. 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. This is in Java but should be easy to convert to another language.

    // 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. It seems like a pretty accurate solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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