Get Latitude Longitude after x kilometer on Google Map without destination?

流过昼夜 提交于 2020-01-25 22:54:06

问题


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 ?


回答1:


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.



来源:https://stackoverflow.com/questions/31077661/get-latitude-longitude-after-x-kilometer-on-google-map-without-destination

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!