- ## Javascript
- ## This is in application.js
- var map;
- var directions;
- function initialize() {
- if (google.maps.BrowserIsCompatible()) {
- map = new google.maps.Map2(document.getElementById("map_canvas"));
- map.setCenter(new google.maps.LatLng(37.4419, -122.1419), 13);
- map.setUIToDefault();
- directions = new google.maps.Directions(map);
- google.maps.Event.addListener(directions, "load", onGDirectionsLoad);
- }
- }
- function getdirections(address1, address2) {
- var string = "from: " + address1 + " to: " + address2;
- directions.load(string);
- }
- function onGDirectionsLoad() {
- document.getElementById("getDistance").innerHTML =
- directions.getDistance().html;
- }
- ____
- ## public/javascripts/directions.js [javascript]
- jQuery(document).ready(function() {
- // code in here will be executed once the DOM is loaded
- jQuery("form").submit(function(submit_event) {
- // prevent default submitting of the form
- submit_event.preventDefault();
- // do whatever we want when the form is 'submitted'
- // where are you getting the addresses from?
- getdirections();
- });
- });
- ## app/view/layouts/application.html.erb
- <html>
- <head>
- <%= include_javascript_tag 'application' %>
- </head>
- <body>
- <%= yield %>
- </body>
- </html>
- ## first attempt with form_remote_tag
- <% form_remote_tag (:update => "distance", :url => { :action => :getdistance } ) do %>
- <div id="waypoints">
- <%= render :partial => 'waypoint', :collection => @waypoints %>
- </div>
- <%= add_waypoint_link "Add Waypoint" %>
- <%= submit_tag 'Get Driving Distance', :name => nil, :onclick => 'getdirections()' %>
- <% end %>
- ____
- ## Second attempt with plain form_tag
- <% form_tag :update => "distance", :class => 'waypoint-form' do %>
- <div id="waypoints">
- <%= render :partial => 'waypoint', :collection => @waypoints %>
- </div>
- <%= add_waypoint_link "Add Waypoint" %>
- <%= submit_tag 'Get Driving Distance', :name => nil %>
- <% end %>