如何使用Google地图查找路线的位置? [英] How to find location along a route with Google Maps?

查看:163
本文介绍了如何使用Google地图查找路线的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



任何人都知道如何做到这一点? http://code.google.com/apis/maps/documentation/flash/reference.html#Route


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


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

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



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



  // ===一种方法,其中返回沿路径给定距离的顶点号=== 
// ===如果路径短于指定的距离,则返回null ===
GPolygon.prototype.GetIndexAtDistance = function(meters ){
//一些尴尬的特殊情况
if(meters == 0)return this.getVertex(0);
if(meters <0)返回null;
var dist = 0;
var olddist = 0; $ i
for(var i = 1;(i olddist = dist;
dist + = this.getVertex(i).distanceFrom(this.getVertex(i-1));
}
if(dist return i;
}

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


I've been able to use the DirectionServices portion of the Google Maps API to find a route between two addresses. Given the starting address and a distance (e.g. 5 miles), I'd like to plot the resulting point (e.g. 5 miles along the route) on the map.

Does anybody know how to accomplish this?

解决方案

Check out http://code.google.com/apis/maps/documentation/flash/reference.html#Route

Objects of this class are created by the Directions object to store information about a single route in a directions result.

This allows you to use the getStep method. Steps provide you with distance, duration, latlng etc.

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

In order to get the point at (e.g.) 5 miles, you could get the two points closest to this distance. From what I understand these points should be line of sight (i.e. point-to-point with no turns) so you should be able to extrapolate the correct point between them using math.

There seems to be a related discussion here: http://groups.google.com/group/google-maps-js-api-v3/browse_thread/thread/a475d03a28865614/23ed9e966d10cdd8?pli=1

Here is an example of an animation which shows the mileage, so I'd be surprised if you couldn't get something working by looping through the points until you've found the value you want (the source on this page should get you started): http://econym.org.uk/gmap/example_cartrip.htm

Sorry I don't have a more concrete answer - I will try and write some code for this when I have some time.

EDIT: if you look at the car_trip example, you will find the following:

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

This should allow you to plot the point as desired.

这篇关于如何使用Google地图查找路线的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆