如何使用ruby获得两个坐标之间的汽车路线?

我正在使用谷歌地图和汽车路线开发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),其中包含一系列方向,包括距离,坐标和书面指令。

Interesting Posts