在Google Maps v3中使用航点处理多条路线

时间:2012-04-13 08:39:12

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

我想用可拖动的航点显示多个方向,并在调用Route.prototype.setWayPoints()时保存与对象或数据库中的原点,目标相关联的每个航点。

这家伙正在使用单一路线和可拖动的航点(并将其保存在数据库中)。

http://vikku.info/programming/google-maps-v3/draggable-directions/saving-draggable-directions-saving-waypoints-google-directions-google-maps-v3.htm

我想要多条路线而不是一条路线。我google了很多但找不到任何东西!

这是我尝试过的。


function Route(origin, destination){
    this.origin = origin; // LatLng
    this.destination = destination; //LatLng
    this.way_points = null;
};

Route.prototype.drawRoute = function(){
                 this.dser.route({'origin': this.origin,
                   'destination': this.destination,
                   'waypoints': this.way_points,
                   'travelMode': google.maps.DirectionsTravelMode.DRIVING},
                   function(res,sts) {
                          if(sts=='OK'){
                              var dren = new google.maps.DirectionsRenderer({ 'draggable':true }); 
                              dren.setMap(map); //global variable 'map'
                              dren.setDirections(res);
                          }   
                  });
};

Route.prototype.setGMap = function(map){
      this.dren.setMap(map);
};

Route.prototype.setWayPoints = function(){
  this.way_points =   //... what should I do?
};


/* --- main part --- */

r0 = new Route(new google.maps.LatLng( 30, 31 ), new google.maps.LatLng( 40, 41 ));
r0.drawRoute();

// User drags and drops the route on the browser

r0.setWayPoints(); // newly added waypoints should be stored in r0.way_points

r1 = new Route(new google.maps.LatLng( 50, 51 ), new google.maps.LatLng( 60, 61));
r1.drawRoute();

// User drags and drops the route on the browser

r1.setWayPoints(); // newly added waypoints should be stored in r1.way_points

有人能告诉我如何实现Route.prototype.setWayPoints,以便当前googlemap的路线中的路点可以存储在Route对象中吗?

1 个答案:

答案 0 :(得分:0)

我稍微改变了方法,并在Route-object中添加了getPoints - 方法。

此方法将返回一个由原点,航点和目标组成的数组,因此您可以轻松地将其传递到某处。 (数组的每个项目都是数组[lat,lng])

http://jsfiddle.net/doktormolle/fjUqK/