Java中的边界框计算

时间:2010-09-10 13:05:03

标签: geolocation

我正在尝试创建一个给定纬度和经度以及范围(以英里或公里为单位)返回边界框的NE和SW角的方法?

我有一个我在某个地方找到的功能,但经过一些测试后它似乎不起作用(见下文):

        double latrange=range/69.172;
    double longrange=Math.abs(range/(Math.cos(inLat) *69.172));
    double minlat=inLat-latrange;
    double maxlat=inLat+latrange;
    double minlon=inLong-longrange;
    double maxlon=inLong+longrange;

    MapCoord min = new MapCoord(minlat,minlon);
    MapCoord max = new MapCoord(maxlat,maxlon);
    MapCoord [] rs = new MapCoord[2];
    rs[0] = min;
    rs[1] = max;
    return rs;  

1 个答案:

答案 0 :(得分:0)

不清楚你的意思是“不起作用”,但从我所看到的代码确实导致输入的两点SW和NE。然而,它将接近极点和国际日期线。

简单的测试程序,产生合理的输出:

public class LatLonBoundBox {

    public static class MapCoord {
        final double lat;
        final double lon;
        public MapCoord(double lat, double lon) {
            this.lat=lat;
            this.lon=lon;
        }
        @Override
        public String toString() {
            return "MapCoord [lat=" + lat + ", lon=" + lon + "]";
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        double range = 1.0; 
        double inLat = 51.350801;
        double inLong = -0.251850;

        double latrange=range/69.172;
        double longrange=Math.abs(range/(Math.cos(inLat) *69.172));
        double minlat=inLat-latrange;
        double maxlat=inLat+latrange;
        double minlon=inLong-longrange;
        double maxlon=inLong+longrange;

        MapCoord min = new MapCoord(minlat,minlon);
        MapCoord max = new MapCoord(maxlat,maxlon);
        System.out.println(min);  
        System.out.println(max);  
    }

}