Guest User

Gravity Forms Map

a guest
Jul 7th, 2011
499
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Dynamic category dropdown and map generation for survey form
  2. add_filter("gform_pre_render_6", "monitor_single_dropdown");
  3. function monitor_single_dropdown($form){
  4.  
  5. ?>
  6. <script type="text/javascript">
  7.  
  8. jQuery(document).ready(function(){
  9.  
  10.         //** Dynamic dropdown code **
  11.         //clone the category dropdown
  12.         var clone_select = jQuery('#input_13').clone();
  13.         //bind change function to state dropdown
  14.         jQuery('#input_6_34').bind('change', function() {
  15.             //get selected state
  16.             var selectedValue = jQuery("#input_6_34").val();
  17.             //filter out everything but bases in that state
  18.             var clone_subset = clone_select.find('option')
  19.                                            .filter('[value="' + selectedValue + '"]')
  20.                                            .nextUntil('.level-0')
  21.                                            .clone();
  22.             jQuery('#input_13').empty().append( clone_subset );
  23.            
  24.             jQuery("#input_13").find("option").each(function (index, option) {
  25.                 jQuery(option).html(jQuery(option).html().replace('&nbsp;&nbsp;&nbsp;',''));
  26.             });
  27.        
  28.         });
  29.        
  30.         //** Geocode city and state to a lat/long and refresh the map
  31.     function ChangeMap() {
  32.  
  33.         var geocoder = new GClientGeocoder();
  34.        
  35.         var address;
  36.        
  37.         if ( !jQuery("#map_search").val() ) {
  38.  
  39.             address = jQuery("#input_13 option:selected").text() + ", " + jQuery("#input_6_34 option:selected").text();
  40.  
  41.         }
  42.  
  43.         else {
  44.  
  45.             address = jQuery("#map_search").val();
  46.  
  47.         }
  48.        
  49.         if ( geocoder ) {
  50.  
  51.             geocoder.getLatLng(
  52.  
  53.                 address,
  54.  
  55.                 function(point) {
  56.  
  57.                     if ( !point ) {
  58.  
  59.                         alert(address + " not found");
  60.  
  61.                     } else {
  62.  
  63.                         map.setCenter(point);
  64.  
  65.                         marker.setPoint(point);
  66.  
  67.                         marker.show();
  68.                        
  69.                         map.setZoom(13);
  70.                        
  71.                         //set hidden form fields to lat/long
  72.                         jQuery("#input_6_32").val(point.lat());
  73.  
  74.                         jQuery("#input_6_33").val(point.lng());
  75.  
  76.                     }
  77.  
  78.                 }
  79.  
  80.             );
  81.  
  82.         }
  83.  
  84.         return false;
  85.     }
  86.    
  87.     //bind dropdowns to ChangeMap function     
  88.     jQuery('#input_13, #input_6_34').bind('change', function() {
  89.         ChangeMap();
  90.     });
  91.    
  92.     //bind search button to ChangeMap function
  93.     jQuery('#map_search_button').click(function() {
  94.         ChangeMap();
  95.         jQuery("#map_search").val("");
  96.     });
  97.        
  98. });
  99. </script>
  100.    
  101. <?php
  102. return $form;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment