Tag Archives: rubyonrails

Geocoding with Ruby On Rails and Google Maps

As GoogleMaps now offers geocoding services (which works really fine for European streets !), you can use it in any web application to get coordinates from a given address. The services use REST to send queries, and you can get results in XML or JSON.

I’m currently hacking with RoR, so I first tested geocoding with a short ruby snippet:

 require 'open-uri'  require 'rubygems'  require 'hpricot'  key = 'xxx'  address = "105+avenue+de+la+Republique,+Paris,+France"  url = "http://maps.google.com/maps/geo?q=#{address}&output=xml&key=#{key}"  open(url) do |file|    @body = file.read    doc = Hpricot(@body)    (doc/:point/:coordinates).each do |link|      long, lat = link.inner_html.split(',')    end  end

I used hpricot to parse XML results, and even if it’s designed for HTML it works fine in this use case.

Yet, my initial goal was to automatically get coordinates from any address in a RoR application. I decided to add longitude and latitude properties to the class I wanted to geolocate, and add a before_save method in the model.

 # Get lat, long from address before save  def before_save    require 'open-uri'    address = CGI::escape("#{self.address},#{self.city},#{self.state},#{self.country}")    url = "http://maps.google.com/maps/geo?q=#{address}&output=xml&key=#{key}"    open(url) do |file|      @body = file.read      doc = Hpricot(@body)      (doc/:point/:coordinates).each do |link|        self.longitude, self.latitude = link.inner_html.split(',')      end    end  end

That’s it, every time a user create or edit an instance, its coordinates will be added. Well, I certainly should do more tests, but as my knowledge of ruby and rails is limited at the moment, I’ll start with this. If you think something should be changed, feel free to comment this post :) (btw, can someone tell me why I must use require 'open-uri' here, even if that’s already included in my environment.rb ?)

About these ads

Groumph … (ou comment installer des gems avec Locomotive)

Deux soirs que je m’acharne sur Locomotive, à essayer de comprendre pourquoi les gems que j’installe sont inaccessibles dans mes applis rails.

Et ce soir, la révélation !

 Marvin:~/Work/rails/rateitems alex$ gem install rfuzz  Attempting local installation of 'rfuzz'  Local gem file not found: rfuzz*.gem  Attempting remote installation of 'rfuzz'  Select which gem to install for your platform (i686-darwin8.6.1)   1. rfuzz 0.8 (mswin32)   2. rfuzz 0.8 (ruby)   3. rfuzz 0.7 (ruby)   4. rfuzz 0.7 (mswin32)   5. rfuzz 0.6 (ruby)   6. Cancel installation  > 2  Building native extensions.  This could take a while...  /Applications/Locomotive2/Bundles/rails112.locobundle/i386/lib/ruby/1.8/fileutils.rb:243:    command not found: make  /Applications/Locomotive2/Bundles/rails112.locobundle/i386/lib/ruby/1.8/fileutils.rb:243:    command not found: make install  [...]

Forcément, une compilation sans make, ça marche pas terrible …

Donc, install d’XCode, et c’est reparti:

  Marvin:~/Work/rails/rateitems alex$ gem install rfuzz  Attempting local installation of 'rfuzz'  Local gem file not found: rfuzz*.gem  Attempting remote installation of 'rfuzz'  Select which gem to install for your platform (i686-darwin8.6.1)   1. rfuzz 0.8 (mswin32)   2. rfuzz 0.8 (ruby)   3. rfuzz 0.7 (ruby)   4. rfuzz 0.7 (mswin32)   5. rfuzz 0.6 (ruby)   6. Cancel installation  > 2  Building native extensions.  This could take a while...  dyld: Library not loaded: /usr/i686-apple-darwin8/lib/libgcc_s.1.dylib    Referenced from: /usr/bin/gcc    Reason: image not found  make: *** [http11_client.o] Trace/BPT trap  dyld: Library not loaded: /usr/i686-apple-darwin8/lib/libgcc_s.1.dylib    Referenced from: /usr/bin/gcc    Reason: image not found  [...]

Un petit tour sur google pour au final trouver ce thread et modifier le path

 export DYLD_FALLBACK_LIBRARY_PATH=$DYLD_FALLBACK_LIBRARY_PATH:/usr/lib

Et hop, ca roule ! (même s’il reste quelques /Users/ryan/Desktop/building/min/framework/lib qui doivent être codé en dur quelque part).

Allez, un bug report histoire de signaler ça, et je retourne jouer avec rails.

Firsts steps with Ruby On Rails

As I mentioned before, I planned to rewrite recordShops in Ruby on Rails. Actually, more that rewriting this particular application, I’ll try to write a generic “locate and review” tool, with usual tagging and Google Maps stuff.

So, let’s talk about RoR. I’ve already looked at it before, but started seriously to play with it today. And that’s really cool!

Reading Four Days in Rails and with only a few lines of code, I got an object oriented item/reviews model with the appropriate editing forms.

  • You want each item to have a name ? Just add validates_presence of :name in your item model and the form won’t validate if name is empty.
  • Reviews need to be at least 20 chars ? Add validates_length_of :review, :minimum=>20
  • And you want a dropdown country list ? Type <%= country_select 'item', 'country' %> in your form.

Nice isn’t it ?

Then, I looked for an authentication plugin and installed auth_manager. This plugin includes e-mail validation of accounts, password reminder, different user levels, protected webpages …

Next step was tagging. Acts_as_taggable seems to be the way to add tag features to items.

I still need to integrate Google Maps (Cartographer plugin uses v1 API so I’ll have to look at its code before making it run with the new one), but at the moment, I’m really impressed with RoR features to easilly write “basic” online apps.