Adding distance to a GPS coordinate

Solution 1:

  • P0(lat0,lon0) : initial position (unit : degrees)
  • dx,dy : random offsets from your initial position in meters

You can use an approximation to compute the position of the randomized position:

 lat = lat0 + (180/pi)*(dy/6378137)
 lon = lon0 + (180/pi)*(dx/6378137)/cos(lat0)

This is quite precise as long as the random distance offset is below 10-100 km

Edit: of course in Java Math.cos() expects radians so do use Math.cos(Math.PI/180.0*lat0) if lat0 is in degrees as assumed above.

Solution 2:

To take a square I'm using this:

 private double[] getBoundingBox(final double pLatitude, final double pLongitude, final int pDistanceInMeters) {

    final double[] boundingBox = new double[4];

    final double latRadian = Math.toRadians(pLatitude);

    final double degLatKm = 110.574235;
    final double degLongKm = 110.572833 * Math.cos(latRadian);
    final double deltaLat = pDistanceInMeters / 1000.0 / degLatKm;
    final double deltaLong = pDistanceInMeters / 1000.0 / degLongKm;

    final double minLat = pLatitude - deltaLat;
    final double minLong = pLongitude - deltaLong;
    final double maxLat = pLatitude + deltaLat;
    final double maxLong = pLongitude + deltaLong;

    boundingBox[0] = minLat;
    boundingBox[1] = minLong;
    boundingBox[2] = maxLat;
    boundingBox[3] = maxLong;

    return boundingBox;
}

This returns an array with 4 coordinates, with them you can make a square with your original point in center.