Got an iPhone or iPad? We have a brand new Pastebin App for both devices, and it's totally free! Click here to download the new Pastebin App for iOS.
Guest

Untitled

By: a guest on Feb 12th, 2012  |  syntax: None  |  size: 2.01 KB  |  hits: 35  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. ## Javascript
  2. ## This is in application.js
  3.  
  4. var map;
  5. var directions;
  6.  
  7. function initialize() {
  8.     if (google.maps.BrowserIsCompatible()) {
  9.         map = new google.maps.Map2(document.getElementById("map_canvas"));
  10.         map.setCenter(new google.maps.LatLng(37.4419, -122.1419), 13);
  11.         map.setUIToDefault();
  12.         directions = new google.maps.Directions(map);
  13.         google.maps.Event.addListener(directions, "load", onGDirectionsLoad);
  14.     }
  15. }
  16.  
  17.  
  18. function getdirections(address1, address2) {
  19.     var string = "from: " + address1 + " to: " + address2;
  20.     directions.load(string);
  21. }
  22.  
  23. function onGDirectionsLoad() {
  24.  document.getElementById("getDistance").innerHTML =
  25.         directions.getDistance().html;
  26. }
  27.  
  28. ____
  29.  
  30. ## public/javascripts/directions.js [javascript]
  31.  
  32. jQuery(document).ready(function() {
  33.   // code in here will be executed once the DOM is loaded
  34.  
  35.   jQuery("form").submit(function(submit_event) {
  36.     // prevent default submitting of the form
  37.     submit_event.preventDefault();
  38.  
  39.     // do whatever we want when the form is 'submitted'
  40.    
  41.     // where are you getting the addresses from?
  42.     getdirections();
  43.   });
  44. });
  45.  
  46. ## app/view/layouts/application.html.erb
  47. <html>
  48.   <head>
  49.     <%= include_javascript_tag 'application' %>
  50.   </head>
  51.   <body>
  52.     <%= yield %>
  53.   </body>
  54. </html>
  55.  
  56. ## first attempt with form_remote_tag
  57.  
  58. <% form_remote_tag (:update => "distance", :url => { :action => :getdistance }  ) do %>
  59.         <div id="waypoints">
  60.         <%= render :partial => 'waypoint', :collection => @waypoints %>
  61.         </div>
  62.         <%= add_waypoint_link "Add Waypoint" %>
  63.         <%= submit_tag 'Get Driving Distance', :name => nil, :onclick => 'getdirections()' %>
  64. <% end %>
  65.  
  66. ____
  67.  
  68. ## Second attempt with plain form_tag
  69.  
  70. <% form_tag :update => "distance", :class => 'waypoint-form' do %>
  71.         <div id="waypoints">
  72.         <%= render :partial => 'waypoint', :collection => @waypoints %>
  73.         </div>
  74.         <%= add_waypoint_link "Add Waypoint" %>
  75.         <%= submit_tag 'Get Driving Distance', :name => nil %>
  76. <% end %>