如何使用Google地图查找路线上的位置?

Nat*_*ate 3 google-maps

我已经能够使用Google Maps API的DirectionServices部分来查找两个地址之间的路由.给定起始地址和距离(例如5英里),我想在地图上绘制结果点(例如沿着路线5英里).

有人知道怎么做到这一点吗?

Aar*_*ton 7

请查看http://code.google.com/apis/maps/documentation/flash/reference.html#Route

此类的对象由Directions对象创建,用于存储有关路线结果中单个路线的信息.

这允许您使用getStep方法.步骤为您提供距离,持续时间,latlng等.

http://code.google.com/apis/maps/documentation/flash/reference.html#Step

为了得到(例如)5英里的点,你可以得到最接近这个距离的两个点.根据我的理解,这些点应该是视线(即点到点没有转弯),所以你应该能够使用数学推断它们之间的正确点.

这里似乎有一个相关的讨论:http: //groups.google.com/group/google-maps-js-api-v3/browse_thread/thread/a475d03a28865614/23ed9e966d10cdd8?pli = 1

这是一个显示里程数的动画示例,所以如果你找不到你想要的值直到找到所需的值,你就会感到惊讶,如果你无法通过积分来完成工作(这个页面上的来源应该让你开始):http: //econym.org.uk/gmap/example_cartrip.htm

对不起,我没有更具体的答案 - 当我有时间的时候,我会尝试为此写一些代码.

编辑:如果您查看car_trip示例,您将找到以下内容:

// === A method which returns the Vertex number at a given distance along the path ===
// === Returns null if the path is shorter than the specified distance ===
GPolygon.prototype.GetIndexAtDistance = function(metres) {
  // some awkward special cases
  if (metres == 0) return this.getVertex(0);
  if (metres < 0) return null;
  var dist=0;
  var olddist=0;
  for (var i=1; (i < this.getVertexCount() && dist < metres); i++) {
    olddist = dist;
    dist += this.getVertex(i).distanceFrom(this.getVertex(i-1));
  }
  if (dist < metres) {return null;}
  return i;
}
Run Code Online (Sandbox Code Playgroud)

这应该允许您根据需要绘制点.