按距离从方向服务检索目的地坐标

发布于 2024-12-09 20:58:54 字数 256 浏览 1 评论 0原文

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

所以我想我的问题如下:是否可以通过使用方向服务或任何替代方法指定距离(以公里为单位)来检索目的地经纬度坐标(基于/计算道路的距离而不是方向/直线)方式?

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

I need to retrieve a destination's coordinates using the google maps api directions service. I already have the starting point coordinates, however instead of specifying an ending point in coordinates, I wish to retrieve the coordinates by specifying a distance (in km).

So I guess my question is the following: is it possible to retrieve the destination latlong coordinates (based/calculated on the road's distance and not directional/straight line) by specifying a distance (amount in km) with the directions service or perhaps any alternative way?

I have an image illustration, however unfortunately am unable to attach to this question as I do not have enough reputation. If my question is unclear in any way, or you wish to see the illustration then please contact me and I'll send it off.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吃素的狼 2024-12-16 20:58:54

我认为您不能按照请求来执行此操作参数表示起始点和目的地参数是必需的。

I don't think you can do this as the request parameters say that origin and destination parameters are required.

聚集的泪 2024-12-16 20:58:54

我相信它会对某人有所帮助。

Google 地图库:

google.maps.geometry.spherical.computeOffset(fromCoordinates, distanceInMeters, headingInDegrees)

I beliave it will help someone.

There is a method to get coordinates in the google maps library:

google.maps.geometry.spherical.computeOffset(fromCoordinates, distanceInMeters, headingInDegrees)
呆° 2024-12-16 20:58:54

我相信你是对的。 api 中似乎没有任何当前方法可以让您执行以下操作。

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

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

幸运的是,对于我的情况,我只需要计算点 A 和点 A 之间的距离。 B 在一条单独的道路上,所以当您使用多条或通用道路/位置时,我的替代方案将不起作用。鉴于您乐于接受聚合数据和不准确的计算,您可以改用所描述的第一种方法。

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

1.计算两个坐标之间的距离(以米为单位)

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);
}

2.获取最终 AB 坐标的坐标(基于剩余百分比)。 “matrix”变量是一个 json 坐标数组。

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;
}

3.将数字度数转换为弧度

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

希望以下内容可以帮助任何遇到相同或类似问题的人。我没有调查这个,因为我没有必要,但是,也许如果你正在处理谷歌的付费服务,他们不会聚合调用返回的数据?

I believe you are correct. There doesn't seem to be any current method in the api which would allow you to do the following.

Instead I looped through the coordinates returned from the directions service call, and used a function to calculate the distance between coordinates. However even this was not accurate enough as the coordinates returned also seemed to be aggregated and doesn't return an accurate value/distance when calculating the distances between each coordinate as they are aggregated and therefore each coordinate is not necessary along the road.

To work around the above issue, I ended up adding a click event, and plotted the coordinates along the road myself and then stored them in a local json file which I cache and call using an xmlhttprequest.

Fortunately, for my situation I only need to calculate the distance between point A & B on one individual road, so my alternative won't work in cases when you're using multiple or generic roads/locations. You could instead use the first method described, given that you're happy to live with the aggregated data and an in-accurate calculation.

Below are the functions used to calculate the distances between coordinates and then also the final calculation to find the point & coordinates between the final two points. Please note this code relies on and uses jQuery methods.

1. Calculate distance (in meters) between two coordinates

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);
}

2. Fetch coordinate of final A-B coordinate (based on percentage remaining). The 'matrix' variable is a json array of coordinates.

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;
}

3. Convert numeric degrees to radians

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

Hope the following helps any one with the same or simular problem. I haven't investigated this as I've had no need to, but, perhaps if you're dealing with google's paid services, they don't aggregate the data returned by the call?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文