tags:

views:

25

answers:

1

I'm trying to create a method that given a latitude and longitude and range (in miles or km) returns the NE and SW corners of a bounding box?

I had a function that I found somewhere but after some testing it doesn't seem to work( see below):

        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;  
A: 

Not clear what you mean by "doesn't work" but from what I can see the code does result in two points SW and NE of the input. It will however break close to the poles and the international dateline.

Simple test program that produces reasonable looking output:

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);  
    }

}
John Pickup
My issue was when I put a lat/lon for portland oregon and a 20 mile radius, it gave me what was really a couple of hundred mile radius.
fsa317