如何使用红宝石在两个坐标之间获取汽车路线



我正在开发带有谷歌地图和汽车路线的rails应用程序。

我有一个汽车路线的起点和终点 - 两个坐标。如何获得完整的汽车路线(汽车通过街道的坐标数组)?我可以使用gmaps4railsgeocoder来做到这一点吗?

像这样:

start_point = [41,2423; 45,323452]
end_point   = [42,2423; 42,323452]
full_route = some_method_for_calculate_route( start_point, end_point )
#paint line on map from array of points
paint_line( full_route )

请帮忙,谢谢:)

如果你想在

客户端做,这里有一个plunkr。

它受到此处谷歌文档的高度启发。

基本上,这是一种与谷歌图书馆交互的方式

  var handler = Gmaps.build('Google');
  handler.buildMap({ internal: {id: 'map'}}, function(){
    var start_point = [43.2423, 5.323452];
    var end_point   = [44.2423, 5.323452];
    var directionsService = new google.maps.DirectionsService();
    var directionsDisplay = new google.maps.DirectionsRenderer();
    directionsDisplay.setMap(handler.getMap());
    var request = {
      origin:      new google.maps.LatLng(start_point[0], start_point[1]),
      destination: new google.maps.LatLng(end_point[0], end_point[1]),
      travelMode:  google.maps.TravelMode.DRIVING
    };
    directionsService.route(request, function(response, status) {
      if (status === google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
      }
    });
  });

您将需要使用 Google Directions API。这将返回一个 JSON 对象(或 XML),其中包含一系列方向,包括距离、坐标和书面说明。

最新更新