How to query my DB with with javascript?
I have a page which allows users to search for their locations using the google maps API, and I want to be able to search for places I have stored in my DB that are close to the searched location.
So how can I query the DB on the fly and have the google map update?
here is my code for geocoding thus far:
function codeAddress(address) {
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
latLng = results[0].geometry.location;
map.setCenter(results[0].geometry.location);
result=results[0].address_components;
var info=[];
for(var i=0;i<result.length;++i)
{
if(result[i].types[0]=="administrative_area_level_1"){state = i.long_name; }
if(result[i].types[0]=="locality"){city = i.long_name;}
}
......
So how can I take those city and state variables and pass them to my controller to look for records?
EDIT - Im not using jquery on this page at all, the google maps API is plain ol' javascript.
I have zero AJAX knowledge, and very very little javascript knowledge.
So like, if I have开发者_StackOverflow the following variables: var state ="MD"
and var City = "Bethesda"
and my controller so far is:
def shops
@maps = Map.where("state LIKE ?", "%#{params[:state]}%" and "city LIKE ?", "%#{params[:city]}%")
end
And then I have to get that array back and loop through it all to display it.
And I have no idea how to do this, Ive seen numerous AJAX examples with rails but so far nothing has been particularly helpful..
You can use jQuery to make AJAX calls that are portable cross browsers. jQuery is a lightweight JavaScript library. The server will reply with JSON encoded data that in turn will come from the DB. You can use any server side language to do that, such as Ruby, as you suggest from your tags.
Just google for JSON and jQuery.getJson() tutorial and you'll find plenty, such as this one.
精彩评论