Bha*_*ath 0 android google-maps geolocation latitude-longitude google-directory-api
我正在创建一个Android应用程序,需要在X公里后找到同一路线上的坐标.
我有两个坐标x1,y1和x2,y2道路上.现在,我的要求是在同一条道路上找到x3,y33公里后的坐标(即x2,y2不在x1,y1&之间的坐标x2,y2).
怎么能实现这一目标?
如果知道方位,则可以计算目标坐标.
示例代码:
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));
}
Run Code Online (Sandbox Code Playgroud)
样品用法:
double radiusInKM = 10.0;
double bearing = 90;
LatLng destinationPoint = getDestinationPoint(new LatLng((25.48, -71.26), bearing, radiusInKM);
Run Code Online (Sandbox Code Playgroud)
或者您可以在pointA和pointB之间使用标题而不是轴承:
LatLng destinationPoint = getDestinationPoint(new LatLng(37.4038194,-122.081267), SphericalUtil.computeHeading(new LatLng(37.7577,-122.4376), new LatLng(37.4038194,-122.081267)), radiusInKM);
Run Code Online (Sandbox Code Playgroud)
该SphericalUtil.computeHeading(p1, p2);方法来自Android Google Maps Utility库.
这基于此Stackoverflow答案中的Javascript方法.
如果你想在同一条路上找到这个点,你可以查看这个PHP答案.