Skip to content

Instantly share code, notes, and snippets.

@chrise86
Forked from kurioscreative/search.html.haml
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrise86/58adab1f3664ca3d36af to your computer and use it in GitHub Desktop.
Save chrise86/58adab1f3664ca3d36af to your computer and use it in GitHub Desktop.
Geocoder: Over query limit Javascript Fix
= form_tag :search, method: :get, id:'search' do |f|
= text_field_tag :q, '', placeholder:'City, address, or zip code'
= hidden_field_tag :c, ''
= submit_tag "Go"
function loadGeocoder() {
var searchForm = $('form#search');
var query = searchForm.find('input[name="q"]');
var geocoder = new google.maps.Geocoder();
var coordinates = searchForm.find('input[name="c"]');
query.change(function() { coordinates.val(""); }); // clear coordinates on query change
searchForm.submit(function(event) {
if (coordinates.val() === "") {
event.preventDefault();
if ( geocoder ) {
geocoder.geocode({'address':searchForm.find('input[name="q"]').val()}, function(results, status) {
if ( status == google.maps.GeocoderStatus.OK) {
// Submit with new coordinates
coordinates.val(results[0].geometry.location);
searchForm.submit();
}
});
}
}
});
}
function loadScript() {
if ( typeof google === 'undefined' ) {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "http://maps.googleapis.com/maps/api/js?<%= "key=#{ENV['MAPS_API_KEY']}&" if Rails.env.production? %>sensor=false&callback=loadGeocoder";
document.body.appendChild(script);
}
else {
loadGeocoder();
}
}
window.onload = loadScript;
class SearchController < ApplicationController
def search
Place.near(params[:search][:c] || params[:search][:q])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment