我正在使用谷歌地图和汽车路线开发rails应用程序。
我有一个汽车路线的起点和终点 - 两个坐标。如何获得完整的汽车路线(汽车通过街道的坐标系列)?我可以使用gmaps4rails
和geocoder
来完成吗?
这样的事情:
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 )
请帮忙, 谢天谢地:)
答案 0 :(得分:3)
如果您想在客户端进行操作,则可以使用plunkr here。
受google documentation here的启发。
基本上它是一种与谷歌图书馆互动的方式
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);
}
});
});
答案 1 :(得分:1)
您需要使用the Google Directions API。这将返回一个JSON对象(或XML),其中包含一系列方向,包括距离,坐标和书面说明。