谷歌地图:从已知起始位置和给定公里获取纬度/经度

时间:2014-04-07 01:33:50

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

假设我知道A高速公路的起始纬度/路线,建筑区域的起始高度为175公里。 有没有办法计算公里175的纬度/经度? (可能是方向服务)

1 个答案:

答案 0 :(得分:1)

Mike Williams为名为Google Maps Javascript API v2 (now deprecated and turned off)epoly写了一个扩展程序,其中包含方法.GetPointAtDistance,可以执行您想要的操作。

example using a ported version of that library

GetPointAtDistance函数的部分源代码(取决于使用distanceFrom方法扩展google.maps.LatLng对象)

/* .GetPointAtDistance() returns a google.maps.LatLng at the specified distance   *
 *                   along the path.                                              *
 *                   The distance is specified in metres                          *
 *                   Returns null if the path is shorter than that                *
 */

// === A method which returns a google.maps.LatLng of a point a given distance along the path ===
// === Returns null if the path is shorter than the specified distance ===
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 += this.getPath().getAt(i).distanceFrom(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);
}