我如何使用Google Maps API绘制路线?例如,要将一堆路点加载到地图上(我目前有这个),并从每个路线绘制一条线,向用户显示他们可以看到所有这些路线的路线?当用户看到地图时,我该如何加载?
Red*_*ing 29
您可以在DirectionsService对象上设置waypoints属性,它将通过数组中的所有点绘制从源到目标的路径:
中间航点的数组.将通过此阵列中的每个航路点从起点到目的地计算方向.
设置了waypoints属性后,可以调用route方法来计算方向:
route(request:DirectionsRequest, callback:function(DirectionsResult, DirectionsStatus)))
Run Code Online (Sandbox Code Playgroud)
获得DirectionsResult后,您可以使用DirectionsRenderer对象在Google地图上呈现结果.
用工作示例更新
以下代码通过三个航点的数组在硬编码的起点和终点之间进行方向请求:
// 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
};
Run Code Online (Sandbox Code Playgroud)
注意:除非您切换到企业帐户,否则您最多只能在阵列中使用八个航路点.
归档时间: |
|
查看次数: |
63014 次 |
最近记录: |