gpt4 book ai didi

google-maps-api-3 - Google Maps API - route 点

转载 作者:行者123 更新时间:2023-12-01 06:40:42 26 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Midpoint of route in google maps

(1 个回答)


5年前关闭。




我一直在玩谷歌地图/谷歌方向 API。有没有人知道我如何能够找到沿路线的中点,而不是地理中点。

理想情况下,我想找到这个中点的经纬度值。

有什么想法吗?我有点难过,希望我能找到一个建议,而不会疯狂地试图自己找到答案。

最佳答案

您可以使用 Gisgraphy 中的 GetPointAtDistance 原型(prototype).原型(prototype)返回沿折线指定距离的 LatLng。以下代码:

  • 定义折线
  • 确定这条折线的半长
  • 使用原型(prototype)返回中点 LatLng
  • 从中点 LatLng
  • 中提取 Lat 和 Lng

    var polyline = new google.maps.Polyline({
    path: [ new google.maps.LatLng(..., ...),
    new google.maps.LatLng(..., ...),
    ... ];
    }), //1.
    midDistanceLength = polyline.getPath().getLength() / 2, //2.
    midDistanceLatLng = polyline.GetPointAtDistance(midDistanceLength),//3.
    midDistanceLat = midDistanceLatLng.lat(), //4.
    midDistanceLng = midDistanceLatLng.lng(); //4.

    //The prototype from Gisgraphy:
    google.maps.Polygon.prototype.GetPointAtDistance = function(metres) {
    // some awkward special cases
    if (metres == 0) return this.getPath().getAt(0);
    if (metres < 0) return null;
    if (this.getPath().getLength() < 2) return null;
    var dist=0;
    var olddist=0;
    for (var i=1; (i < this.getPath().getLength() && dist < metres); i++) {
    olddist = dist;
    dist += google.maps.geometry.spherical.computeDistanceBetween (
    this.getPath().getAt(i),
    this.getPath().getAt(i-1)
    );
    }
    if (dist < metres) return null;
    var p1= this.getPath().getAt(i-2);
    var p2= this.getPath().getAt(i-1);
    var m = (metres-olddist)/(dist-olddist);
    return new google.maps.LatLng( p1.lat() + (p2.lat()-p1.lat())*m, p1.lng() + (p2.lng()-p1.lng())*m);
    }
    google.maps.Polyline.prototype.GetPointAtDistance = google.maps.Polygon.prototype.GetPointAtDistance;

    关于google-maps-api-3 - Google Maps API - route 点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10694472/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com