Google Maps Directions API - 标记距离起点x公里的点 [英] Google Maps Directions API - Marking a point x km from starting point

查看:207
本文介绍了Google Maps Directions API - 标记距离起点x公里的点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



在GMaps V3中,我可以沿谷歌地图绘制的方向绘制出发点起点x公里的标记。

解决方案

想通了。该技术涉及获取各个纬度的长步并逐步计算距离。各个步骤足够接近以在两个连续点之间进行直线逼近。
上一个超调点完成反向移动。

错误率为0.1%,与距离无关。即在地图上距离2000公里的距离将被关闭2公里。

  //返回距离信息对应的Google LatLng对象给定的路线。 
// Params:directionResult:从DirectionService.route获取的google.maps.DirectionsResult(请求:DirectionsRequest,callback:function(DirectionsResult,DirectionsStatus))
// distanceInKM:相对于初始点。
function getLatLngOnRoute(directionResult,distanceInKM){
var lastPos = directionResult.routes [0] .legs [0] .steps [0] .path [0]; var currPos;
var distance = 0.0;
distanceInKM * = 1000;
//不会考虑备用路线。
for(var j = 0; j< directionResult.routes [0] .legs.length; j ++){
//可能有多条腿,每条腿对应一个方向点。如果没有指定路点,则会有一条单边
(var k = 0; k< directionResult.routes [0] .legs [j] .steps.length; k ++){
//对于(var l = 0; l< directionResult.routes [0] .legs [j] .steps [k] .path.length; l ++){$ b $将有多个子分支或步骤
b currPos = directionResult.routes [0] .legs [j] .steps [k] .path [l];
//计算两个经纬度集之间的距离。
distance + = google.maps.geometry.spherical.computeDistanceBetween(lastPos,currPos);
if(distance> distanceInKM){
//如果精确点位于两点之间,则使用lat-lng转换的距离
var heading = google.maps.geometry.spherical.computeHeading( currPos,lastPos);
var l = google.maps.geometry.spherical.computeOffset(currPos,distance-distanceInKM,heading);
return l;
}
lastPos = currPos;
}
}
}
}

< a href =https://stackoverflow.com/questions/8707473/draw-route-x-kilometers-from-origin>这个话题让我朝着正确的方向前进。


I am trying to build a web application to show the position of users in a map, who are participating in a virtual trek. These treks are long distance one, typically >500km, on highways. The information I know is the trek route (start, end along with way points) and distance he has covered with respect to the starting point.

How in GMaps V3, I can plot the marker, which is x km from the starting point, along the drawn direction by Google maps.

解决方案

Figured out this. The technique involves getting the individual lat long steps and incrementally calculate the distance. The individual steps are close enough to do a straight line approximation between two consecutive points. A reverse traverse is done for the last overshooting point.

The error rate is 0.1%, irrespective of the distance. ie a distance of 2000 km will be off by 2 km on map.

//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 topic set me in the correct direction.

这篇关于Google Maps Directions API - 标记距离起点x公里的点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆