2

I am creating an Android app which requires finding a coordinate on the same route after X kilometers.

I have two coordinates x1,y1 & x2,y2 on a road. Now, my requirement is to find coordinate x3,y3 after some 3 kilometers (i.e., coordinate after x2,y2 not between x1,y1 & x2,y2) on the same road.

How can this be achieved ?

Rakshith Ravi
  • 365
  • 5
  • 16
Bharath
  • 3,001
  • 6
  • 32
  • 65
  • 2
    You are going to have to provide more info on what you want to achieve because there is an infinite number of possible points 3km from a point – tyczj Jun 26 '15 at 16:12
  • @tyczj.. Yeah, there are infinite number of possible points. But, here my requirement is, there is route between two points (x1,y1) & (x2,y2). Now, I want the 3rd point after 3 kilometers on the same route. – Bharath Jun 26 '15 at 16:19
  • Is it just along the same heading established by the two previous lat lons or is it along that road which might be at a different heading 3km down the road? If so then I think you're looking for some sort of navigation library or service. – StuStirling Jun 26 '15 at 16:21
  • @DiscoS2... I need along the same heading established by the two previous lat lons. Which ever is feasible. – Bharath Jun 26 '15 at 16:27
  • 1
    You should first learn what is a route and what is a heading. Your question does not fit to your comments. A coordinate offset by given start coordinate and heading (0-360°) and distance has nothing to do with a route or street or with google maps. Check if you can replace the word "route" in your first sentence with "heading". – AlexWien Jun 26 '15 at 17:03

1 Answers1

11

If you know the bearing, you can calculate the destination coordinate.

Sample Code:

private LatLng getDestinationPoint(LatLng source, double brng, double dist) {
        dist = dist / 6371;
        brng = Math.toRadians(brng);

        double lat1 = Math.toRadians(source.latitude), lon1 = Math.toRadians(source.longitude);
        double lat2 = Math.asin(Math.sin(lat1) * Math.cos(dist) +
                                Math.cos(lat1) * Math.sin(dist) * Math.cos(brng));
        double lon2 = lon1 + Math.atan2(Math.sin(brng) * Math.sin(dist) *
                                        Math.cos(lat1),
                                        Math.cos(dist) - Math.sin(lat1) *
                                        Math.sin(lat2));
        if (Double.isNaN(lat2) || Double.isNaN(lon2)) {
            return null;
        }
        return new LatLng(Math.toDegrees(lat2), Math.toDegrees(lon2));
    }

Sample usage:

   double radiusInKM = 10.0;
   double bearing = 90;
   LatLng destinationPoint = getDestinationPoint(new LatLng((25.48, -71.26), bearing, radiusInKM);

Or you can use heading between your pointA and pointB instead of bearing:

LatLng destinationPoint = getDestinationPoint(new LatLng(37.4038194,-122.081267), SphericalUtil.computeHeading(new LatLng(37.7577,-122.4376), new LatLng(37.4038194,-122.081267)), radiusInKM);

The SphericalUtil.computeHeading(p1, p2); method is from the Android Google Maps Utility library.

This is based on the Javascript method from this Stackoverflow answer.

If you want the point on same road, you might checkout this PHP answer.

Community
  • 1
  • 1
ztan
  • 6,861
  • 2
  • 24
  • 44
  • The question is ambigous, the destion shall be on the road, not just at the bearing. Further since it is unclear what the OP asked, you do not explain what you calculated. This cannot be much of a help for someone who cannot even formulate the question. Furtehr weak points; The unit of distacne is meter, not km. So a function should use meter. The earth radius for WGS84 koordinates as Google and all other use, is more precisly defined, use the WGS84 earth radius, not just the aproximation of 6371 used in hand calculated aviation formulas, where this code comes from – AlexWien Jun 26 '15 at 17:21
  • @ztan.. How can I get the bearing between two coordinates ? – Bharath Jun 26 '15 at 18:55
  • 1
    @TaruniNeema, you can use `computeHeading();` from the Android Maps Utility library, please check my edited answer. – ztan Jun 26 '15 at 21:10
  • @ztan : Hi Ztan, your calculation is not giving me the proper calculated destination point on the same road. May be it is not calculating proper bearing, Sometimes it is giving left side or right side of the road and the destination point is far away as per the distance I have passed in function E.G Point A (24.954540,55.076223), Point B (24.955109,55.076797) and calculated destination point C (24.95612333209401,55.086653040721835). Can you check out this on map so you will have idea about what I'm saying? – Jai Jun 28 '15 at 10:08
  • @AlexWien : Can you help me to find out on the road, not just at the bearing, as per your above comment? – Jai Jun 28 '15 at 10:39
  • @ztan : Hi, Any help? Have you checked my above comment? – Jai Jun 28 '15 at 17:18
  • @ztan.. We tried that solution, its not working all the times. E.g., As per Jai's case also its getting failed.. – Bharath Jun 28 '15 at 20:38
  • @TaruniNeema, you might try the [Google Roads API](https://developers.google.com/maps/documentation/roads/snap), if you want to Snap to road. – ztan Jun 29 '15 at 18:12
  • @ztan.. I tried snap to road, but most of the times it is taking me to the 2nd coordinate(the destination coordinate which is use to find bearing) – Bharath Jun 29 '15 at 18:13
  • @TaruniNeema, [this library](https://github.com/virattt/EnRoute/blob/master/src/com/virat/enroute/models/RouteBoxer.java) might help. You can see [this answer](http://stackoverflow.com/a/17312051/4195406) for more details. – ztan Jun 29 '15 at 19:02