我该如何使用Google Maps API来绘制路线呢?例如,将一堆路径点加载到地图上(我已经做到了),并从它们中的每一个点绘制一条线路,向用户展示他们可以走的路线,以便看到所有这些点?当用户查看地图时,我该如何加载这些路线呢?
我该如何使用Google Maps API来绘制路线呢?例如,将一堆路径点加载到地图上(我已经做到了),并从它们中的每一个点绘制一条线路,向用户展示他们可以走的路线,以便看到所有这些点?当用户查看地图时,我该如何加载这些路线呢?
您可以在 DirectionsService 对象上设置waypoints属性,并通过您的数组中的所有点绘制从源到目的地的路径:
中间路线点的数组。 方向将通过此数组中的每个中间路线点 从起点到目的地计算。
一旦您设置了waypoints属性,您就要调用路线方法来计算方向:
route(request:DirectionsRequest, callback:function(DirectionsResult, DirectionsStatus)))
一旦您获得了 DirectionsResult,您可以使用 DirectionsRenderer 对象将结果渲染在 Google Map 上。
使用有效示例进行更新
以下代码通过三个路标点的数组,在硬编码的起始点和终点之间发出方向请求:
// three points through which the directions pass
var point1 = new google.maps.LatLng(-33.8975098545041,151.09962701797485);
var point2 = new google.maps.LatLng(-33.8584421519279,151.0693073272705);
var point3 = new google.maps.LatLng(-33.84525521656404,151.0421848297119);
// build an array of the points
var wps = [{ location: point1 }, { location: point2 }, {location: point3}];
// set the origin and destination
var org = new google.maps.LatLng ( -33.89192157947345,151.13604068756104);
var dest = new google.maps.LatLng ( -33.69727974097957,150.29047966003418);
var request = {
origin: org,
destination: dest,
waypoints: wps,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
注意: 请记住,除非您切换到商业账户,否则您只能在数组中使用最多八个路标。
&path=color:blue|weight:5|45.123,-123.595|46.456,-124.985
我已经在Android上完成了这个任务。 谷歌有一个相关的服务,这是链接 https://developers.google.com/maps/documentation/roads/intro
您可以在此页面上找到一个API调用的示例,以及一些网络指令。 https://developers.google.com/maps/documentation/roads/snap
如果我没记错的话,每个调用最多可以有100个点,并且无论点数如何,每个调用都会收费。
上述调用还可以配置为捕捉道路。
就地图加载时绘制路径而言,这取决于您使用的技术。我知道在.NET WinForms中很容易实现。