【问题标题】:Google Maps Directions API - Marking a point x km from starting point [duplicate]Google Maps Directions API - 标记距离起点 x 公里的点 [重复]
【发布时间】:2012-06-11 08:08:11
【问题描述】:

我正在尝试构建一个 Web 应用程序来在地图中显示正在参与虚拟跋涉的用户的位置。这些长途跋涉是在高速公路上进行的长途跋涉,通常 > 500 公里。我知道的信息是跋涉路线(起点,终点以及路点)和他相对于起点所走过的距离。

如何在 GMaps V3 中,沿着谷歌地图绘制的方向绘制距离起点 x 公里的标记。

【问题讨论】:

    标签: google-maps-api-3 google-maps-markers


    【解决方案1】:

    想通了。该技术涉及获取各个纬度长步并逐步计算距离。各个步骤足够接近,可以在两个连续点之间进行直线逼近。 最后一个过冲点进行反向遍历。

    无论距离如何,错误率都是 0.1%。即 2000 公里的距离在地图上会偏离 2 公里。

    //Returns the Google LatLng object corresponding to distanceInKM for the given route.
    //Params : directionResult : The google.maps.DirectionsResult obtained from DirectionService.route(request:DirectionsRequest, callback:function(DirectionsResult, DirectionsStatus))
    //distanceInKM : The distance offset with respect to the starting point.
    function getLatLngOnRoute(directionResult, distanceInKM) {
        var lastPos=directionResult.routes[0].legs[0].steps[0].path[0]; var currPos;
        var distance=0.0;
        distanceInKM*=1000;
        //Will not consider alternate routes. 
        for (var j=0; j<directionResult.routes[0].legs.length; j++) {
            //There may be multiple legs, each corresponding to one way point. If there are no way points specified, there will be a single leg
            for (var k=0; k<directionResult.routes[0].legs[j].steps.length; k++) {
                //There will be multiple sub legs or steps
                for (var l=0; l<directionResult.routes[0].legs[j].steps[k].path.length; l++) {
                    currPos=directionResult.routes[0].legs[j].steps[k].path[l];
                    //Calculate the distance between two lat lng sets. 
                    distance+=google.maps.geometry.spherical.computeDistanceBetween(lastPos, currPos);
                    if (distance>distanceInKM) {
                        //If the exact point comes between two points, use distance to lat-lng conversion
                        var heading=google.maps.geometry.spherical.computeHeading(currPos, lastPos);
                        var l= google.maps.geometry.spherical.computeOffset(currPos, distance-distanceInKM, heading);
                        return l;
                    }
                    lastPos=currPos;
                }
            }
        }
    }
    

    This 主题为我指明了正确的方向。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-24
      • 2015-07-24
      • 1970-01-01
      • 1970-01-01
      • 2016-09-23
      • 1970-01-01
      • 1970-01-01
      • 2019-03-09
      相关资源
      最近更新 更多