0

I am working on an Android app and I have latitude and longitude of a geographic point and I would like to get coordinates of the place located from X meters (North) and Y meters (West). GeoPoint does not implement this kind of functionality so I have to find a way to do it by myself...

I made some searches but I did not find anything interesting, do you have any idea how to proceed?

Dan D.
  • 73,243
  • 15
  • 104
  • 123
vital
  • 1,308
  • 3
  • 13
  • 28

2 Answers2

2

Tricky, because the distance between two whole longitudes depends on the latitude, and the distance also depends on the altitude...

To make things simple, I'd try and guess the longitude and latitude, and check the distance between the guess and the origin point. This is easier because getting the distance between two points has already been done.

Start with one axis, and do a kind of binary search until you find the location X meters to the north. Then do the same for the other axis.

Community
  • 1
  • 1
zmbq
  • 38,013
  • 14
  • 101
  • 171
1

With the following method you will get the latitude and longitude to add to the original location to get to the final one. Have in mind that this only works over relatively small distances, as it is ignoring the earth curvature.

private static final float GAP_LAT_LON = 0.00005f; //aprox 5 meters
private float results[] = new float[1];

private PointF getGaps(Location initial, float distanceX, float distanceY, PointF gaps){
    Location.distanceBetween(initial.getLatitude(), initial.getLongitude(), initial.getLatitude(), initial.getLongitude() + GAP_LAT_LON, results);
    double factorX = GAP_LAT_LON / results[0];
    Location.distanceBetween(initial.getLatitude(), initial.getLongitude(), initial.getLatitude() + GAP_LAT_LON, initial.getLongitude(), results);
    double factorY = GAP_LAT_LON / results[0];
    if(gaps == null)
        gaps = new PointF();
    gaps.set((float)(distanceX * factorX), (float)(distanceY * factorY));
    return gaps;
}

//to use
private void teste(){
    PointF gaps = null;
    Location initial = new Location("");

    initial.setLatitude(23.5);
    initial.setLongitude(13.2);

    //100 meters West/East and 300 meters North/South
    getGaps(initial, 100, 300, gaps);

    //gaps.x returns x offset to add/subtract to initial latitude
    //gaps.y returns y offset to add/subtract to initial longitude
}

good luck

Luis
  • 11,978
  • 3
  • 27
  • 35
  • You can tweak it choosing a value to GAP_LAT_LON that is closer to to values of distance that you are working with. You can also increase precision if you return the gaps as double, but you will have to define your own class as PointD (double) doesn't exist. – Luis Sep 29 '12 at 21:15