Rails:Gmaps4rails获取路线并在地图上显示

我已经在我的应用上实现了gem 'gmaps4rails' 。 我在动态标签内显示一个带有两个标记的地图,一个用于当前位置(dd-start.png),另一个用于目标位置(dd-end.png)。

这是当前的设置:

 def show @item = Item.find(params[:id]) respond_to do |format| format.html format.js { render 'show.js.erb' } end @hash = Gmaps4rails.build_markers(@item) do |item, marker| marker.lat item.latitude marker.lng item.longitude marker.picture({ :url => "https://www.google.com/mapfiles/dd-end.png", :width => 32, :height => 32 }) marker.infowindow render_to_string(:partial => 'pages/info_page') end append_cur_location end # method to show current location marker on goggle maps def append_cur_location @hash <action[0], :lng=>action[1], :picture=> { :url=> "https://www.google.com/mapfiles/dd-start.png", :width=> 32, :height=> 32 } } end #method that gets html5 geolocation cookies and splits it def action @lat_lng = cookies[:lat_lng].split("|") unless cookies[:lat_lng] == nil end 

在视图页面中,我有以下内容:

 
handler = Gmaps.build('Google'); handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){ markers = handler.addMarkers(); handler.bounds.extendWith(markers); handler.fitMapToBounds(); handler.getMap().setZoom(15); });

此时我想添加目标function,显示两点之间的线路。 在此处输入图像描述

更新1

这就是我现在要做的。 我正在尝试获取lal_lan cookie并设置当前位置。 我设置的用于检查cookie值的警报显示为42.850033 | 85.6500523,因此无法正常工作。

有关如何使这项工作的任何想法???

  function getCookie(name) { var value = "; " + document.cookie; var parts = value.split("; " + name + "="); if (parts.length >= 2) return parts.pop().split(",").shift(); } var directionsDisplay = new google.maps.DirectionsRenderer(); var directionsService = new google.maps.DirectionsService(); function calcRoute() { var origin = getCookie('lat_lng'); alert(origin); var destination = new google.maps.LatLng(42.850033, -85.6500523); var request = { origin: origin, destination: destination, travelMode: google.maps.TravelMode.DRIVING }; directionsService.route(request, function(response, status) { if (status == google.maps.DirectionsStatus.OK) { directionsDisplay.setDirections(response); } }); } calcRoute(); handler = Gmaps.build('Google'); handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){ directionsDisplay.setMap(handler.getMap()); markers = handler.addMarkers(); handler.bounds.extendWith(markers); handler.fitMapToBounds(); handler.getMap().setZoom(15); });