Google maps 按距离从方向服务检索目标坐标

Google maps 按距离从方向服务检索目标坐标,google-maps,coordinates,distance,directions,Google Maps,Coordinates,Distance,Directions,我需要使用谷歌地图api方向服务检索目的地的坐标。我已经有了起点坐标,但是我希望通过指定以km为单位的距离来检索坐标,而不是在坐标中指定终点 因此,我想我的问题如下:是否可以通过使用方向服务或任何其他方式指定以公里为单位的距离量,来检索基于道路距离而非方向/直线的目的地latlong坐标 我有一个图像插图,但不幸的是,我无法附加到这个问题,因为我没有足够的声誉。如果我的问题在任何方面都不清楚,或者您希望看到插图,那么请与我联系,我会将其发送出去。我认为您不能这样做,因为必须提供始发地和目的地参数

我需要使用谷歌地图api方向服务检索目的地的坐标。我已经有了起点坐标,但是我希望通过指定以km为单位的距离来检索坐标,而不是在坐标中指定终点

因此,我想我的问题如下:是否可以通过使用方向服务或任何其他方式指定以公里为单位的距离量,来检索基于道路距离而非方向/直线的目的地latlong坐标


我有一个图像插图,但不幸的是,我无法附加到这个问题,因为我没有足够的声誉。如果我的问题在任何方面都不清楚,或者您希望看到插图,那么请与我联系,我会将其发送出去。

我认为您不能这样做,因为必须提供始发地和目的地参数。

我相信您是正确的。api中似乎没有任何当前方法允许您执行以下操作

相反,我遍历了从directions服务调用返回的坐标,并使用一个函数来计算坐标之间的距离。但是,即使这样也不够精确,因为返回的坐标似乎也被聚合,并且在计算聚合后的每个坐标之间的距离时,不会返回准确的值/距离,因此道路沿线不需要每个坐标

为了解决上述问题,我最后添加了一个click事件,并绘制了道路沿线的坐标,然后将它们存储在本地json文件中,并使用xmlhttprequest缓存和调用该文件

幸运的是,就我的情况而言,我只需要计算一条道路上A点和B点之间的距离,因此,在使用多条或通用道路/位置的情况下,我的备选方案不起作用。您可以使用所描述的第一种方法,因为您乐于接受聚合数据和不精确的计算

以下是用于计算坐标之间距离的函数,以及用于查找最终两点之间的点和坐标的最终计算。请注意,此代码依赖并使用jQuery方法

一,。计算两个坐标之间的距离(以米为单位)

二,。根据剩余百分比获取最终A-B坐标的坐标。“矩阵”变量是一个json坐标数组

三,。将数字度转换为弧度


希望以下内容能帮助任何有相同或类似问题的人。我没有对此进行调查,因为我没有必要这样做,但是,如果你在处理谷歌的付费服务,他们可能不会汇总电话返回的数据?

我相信你是对的。api中似乎没有任何当前方法允许您执行以下操作。您已将问题的答案标记为正确。你的回答甚至没有回答所问的问题。此外,您可以很容易地将distance=google.maps.geometry.spheremic.ComputedDistanceBetweenPointA,pointB;
function pointDistance( begin, end )
{
    var begin   = { lat: begin[0], long: begin[1] },
        end     = { lat: end[0], long: end[1] };

    // General calculations
    var earthRadius     = 6371, //km
        distanceLat     = (end.lat - begin.lat).toRad(),
        distanceLong    = (end.long - begin.long).toRad();

    // Convert lats to radiants
    begin.lat   = begin.lat.toRad();
    end.lat     = end.lat.toRad();

    // Calculation
    var a = Math.sin(distanceLat / 2) * Math.sin(distanceLat / 2) +
            Math.sin(distanceLong / 2) * Math.sin(distanceLong / 2) * Math.cos(begin.lat) * Math.cos(end.lat);

    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); 
    var distance = (earthRadius * c) - 0.000536;

    return (distance * 1000);
}
function getCoordinates( totalDistance )
{
    var lastPoint   = { lat: null, long: null },
        total       = parseFloat(0),
        position    = { start: null, end: null, distance: 0 };

    $(matrix).each(function()
    {
        if ( lastPoint.lat == null )
        {
            lastPoint = { lat: this[0], long: this[1] };
            return;
        }

        var distance = pointDistance([lastPoint.lat, lastPoint.long], [this[0], this[1]]);
        total = total + distance;

        if ( (total / 1000) >= totalDistance )
        {
            position.start      = new google.maps.LatLng(lastPoint.lat, lastPoint.long);
            position.end        = new google.maps.LatLng(this[0], this[1]);
            position.distance   = total;

            return false;
        }

        lastPoint = { lat: this[0], long: this[1] };
    });

    return position;
}
if ( typeof(Number.prototype.toRad) === 'undefined' ) {
    Number.prototype.toRad = function() {
        return this * Math.PI / 180;
    }
}