【问题标题】:Google Maps API - Midpoint along route [duplicate]Google Maps API - 路线中点[重复]
【发布时间】:2012-05-22 00:48:37
【问题描述】:

我一直在使用 Google 地图/Google 路线 API。有没有人知道我如何能够找到沿路线的中点,而不是地理中点。

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

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

【问题讨论】:

    标签: google-maps-api-3 map-directions


    【解决方案1】:

    您可以使用来自Gisgraphy 的GetPointAtDistance 原型。原型返回沿折线指定距离的 LatLng。以下代码:

    1. 定义折线
    2. 确定这条折线的一半长度
    3. 使用原型返回中点LatLng
    4. 从中点 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;
    

    【讨论】:

      【解决方案2】:

      最简单的方法是你可以先:

      1) 使用 GMSGeometryDistance 计算路径的总距离,通过 GMSGeometryDistance 函数计算每两个后续点之间的距离,然后将所有距离相加。

      2) 然后你再次计算,并在每一步求和。当总和约为总距离的一半时,您就在中间点。 示例代码如下:

          func findTotalDistanceOfPath(path: GMSPath) -> Double {
      
              let numberOfCoords = path.count()
      
              var totalDistance = 0.0
      
              if numberOfCoords > 1 {
      
                  var index = 0 as UInt
      
                  while index  < numberOfCoords{
      
                      //1.1 cal the next distance
      
                      var currentCoord = path.coordinateAtIndex(index)
      
                      var nextCoord = path.coordinateAtIndex(index + 1)
      
                      var newDistance = GMSGeometryDistance(currentCoord, nextCoord)
      
                      totalDistance = totalDistance + newDistance
      
                      index = index + 1
      
                   }
      
              }
      return totalDistance
      
          }
      
      func findMiddlePointInPath(path: GMSPath ,totalDistance distance:Double) -> CLLocationCoordinate2D? {
      
          let numberOfCoords = path.count()
      
          let halfDistance = distance/2
      
          let threadhold = 10 //10 meters
      
          var midDistance = 0.0
      
          if numberOfCoords > 1 {
      
              var index = 0 as UInt
      
              while index  < numberOfCoords{
      
                  //1.1 cal the next distance
      
                  var currentCoord = path.coordinateAtIndex(index)
      
                  var nextCoord = path.coordinateAtIndex(index + 1)
      
                  var newDistance = GMSGeometryDistance(currentCoord, nextCoord)
      
                  midDistance = midDistance + newDistance
      
                  if fabs(midDistance - halfDistance) < threadhold { //Found the middle point in route
      
                      return nextCoord
      
                  }
      
                  index = index + 1
      
              }
      
          }
          return nil //Return nil if we cannot find middle point in path for some reason
      }
      

      还有更多功能需要优化。我在herehere

      中用 Swift 写了一个详细的答案

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-14
        • 1970-01-01
        相关资源
        最近更新 更多