简体   繁体   English

在Google Maps API中2个点之间的直线上的特定距离后找到一个点

[英]Find a point after specific distance in straight line between 2 Points in google maps API

I have a straight line from P1 to P2 as shown below, drawn using google maps API. 我有一条从P1到P2的直线,如下所示,它是使用Google Maps API绘制的。 Latitude and Longitude is available for both of these points. 这两个点都可以使用纬度和经度。

I want to find a point in between these straight line say (P) with latitude and longitude which is located after some distance from P1. 我想在经度和纬度这两个直线(P)之间找到一个点,该点位于距P1一段距离之后。

Eg : Say distance between P1 and P2 is 10 km, I want to find a point located 4 km after P1 in a straight line to P2. 例如:假设P1和P2之间的距离为10 km,我想找到一个位于P1之后4 km处,与P2呈直线的点。

在此处输入图片说明

 function markPointInStraightLine(LatLngP1,LatLngP2, distanceInKm) {
       // To do
       //LatLngP1 and LatLngP2 is of type 'google.maps.LatLng'
}

You can use the interpolate function from the google api geometry library 您可以使用Google API几何库中的插值函数

Given two LatLng objects and value between 0 and 1, you may also calculate a destination between them using the interpolate() method, which performs spherical linear interpolation between the two locations, where the value indicates the fractional distance to travel along the path from the origin to the destination. 给定两个LatLng对象,其值在0到1之间,您还可以使用interpolate()方法计算它们之间的终点,该方法在两个位置之间执行球面线性插值,其中该值表示从路径到路径的分数距离从原点到目的地。

more info at google api 谷歌API更多信息

I would suggest using the geometry library of Maps JavaScript API. 我建议使用Maps JavaScript API的几何库 The geometry library provides computeHeading() method to define heading between two points and computeOffset() method to calculate point on the certain distance from an origin in the specified heading. 几何库提供了computeHeading()方法来定义两个点之间的航向,以及computeOffset()方法来计算距指定航向中的原点一定距离的点。 Add &libraries=geometry parameter in Maps JavaScript API URL in order to use the geometry library in your application 在Maps JavaScript API URL中添加&libraries=geometry参数,以便在应用程序中使用几何库

The code snippet should be 该代码段应为

function markPointInStraightLine(LatLngP1,LatLngP2, distanceInKm) {
    var heading = google.maps.geometry.spherical.computeHeading(LatLngP1,LatLngP2);
    var p = google.maps.geometry.spherical.computeOffset(LatLngP1, distanceInKm*1000, heading);
    return p;
}

I hope this helps! 我希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM