How get car route between two coordinates using ruby?

*爱你&永不变心* 提交于 2019-12-23 04:22:12

问题


I'm developing rails app with google maps and car routes.

I have a start and finish points for car route - two coordinates. How can I get a full car route (array of coordinates for car through streets)? Can I do it using gmaps4rails and geocoder?

Something like this:

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 )

Please help, thanks kindly :)


回答1:


If you want to do it client side, you have a plunkr here.

It is highly inspired by the google documentation here.

Basically its a way to interact with google libraries

  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);
      }
    });
  });



回答2:


You will want to use the Google Directions API. This will return you a JSON Object (or XML) with an array of directions including distance, coordinates, and written instructions.



来源:https://stackoverflow.com/questions/27017658/how-get-car-route-between-two-coordinates-using-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!