overstimulate

Geo-comments: Gmaps on Rails

Thu, 09 Jun 2005 google maps rails comments

I’ve spent the last week messing with google maps for various projects. Finally got everything working great.

I’ve modified Typo to have a Geo-comment feature. (when you add a comment you can enter your zip code and it will put your comment on the map!)

Using Google Maps on Rails Model

  • Add lat & lng as a double to your model. * Add a snippet of code to your model to grab the lat,lng from google
  require 'open-uri'
  require 'cgi'

  # ... rest of model ...

  def before_save
    begin
      pattern = /<point lat="([^"]*)" lng="([^"]*)"\/>/
      url = 'http://maps.google.com/?q='+CGI::escape( self.address + ... + ' US' )
      loc = open(url).read.scan(pattern)
      self.lat = loc[0][0]
      self.lng = loc[0][1]
    rescue
      # google says "did you mean . . . ?" if there 
  # were multiple places the address matched.
      # I've been sending them back to the user 
      # asking them to choose
    end
  end

Controllers

You will need a controller to control what the user sees and what google sees. (read the Views section if this doesn’t make sense.)

You will need a controller to process the data for the html page you serve. This controller (probably) doesn’t generate the xml data (see end of post for more discussion), as google will request that later.

Then you need a controller that does a find on your data – nothing new here – passing it to a data view.

Views

What the user sees: (here be magic)

There are two ways to embed google maps, either as a small view or a application view – the map app has keys capture working for easier movement.

What google sees: (the xml view for data)

In the above view, it references a XML file to poll data from, contrary to what you think, you probably DO NOT want to use a .rxml file, as it takes longer than just typing the xml out yourself.

Warning: if your xml isn’t valid google will not process it!

This file looks just like the xml files from the above demos.

XSL (another cool technology) is used to create the views for the maps… Nothing ruby specific here, just take the examples, read some faqs, and make yours do what you want.


Ok, you don’t have to have two seperate views, instead of doing a xmlhttprequest, you can just send the xml (created via a partial) with the main view, and be done with it… To keep things simple, I recommend keeping two seperate views until you are done, then combining them (as checking validity of xml is easier if you don’t have to copy and paste, ...)


Responses to "Geo-comments: Gmaps on Rails"

  1. Thu, 09 Jun 2005 Jesse says:
    Hi from my house, looks like it works!
  2. Thu, 09 Jun 2005 Britt says:
    Fantistically amazing! That's a "tremendous" project!
  3. Thu, 09 Jun 2005 Fred says:
    Testing... Pretty darn cool.
  4. Thu, 09 Jun 2005 Chad Dibble says:
    it is cold up here
  5. Fri, 10 Jun 2005 Jesse says:
    This doesn't work in Safari because of the XSL file.... I need to rewrite it to modify the gmaps structures dynamically like I did for gank (google tank railsday project)...
  6. Fri, 10 Jun 2005 iain says:
    testing lat, long probably won't work, just for fun.
  7. Fri, 10 Jun 2005 roland says:
    cool maps
  8. Fri, 10 Jun 2005 roland says:
    cool maps
  9. Fri, 10 Jun 2005 Jesse Newland says:
    oh I am so using this. sweet man :)
  10. Fri, 10 Jun 2005 Scott Becker says:
    checkin' it out
  11. Fri, 10 Jun 2005 Mitchua says:
    Nothing for the Canadians :( Nice script though :D
  12. Sat, 11 Jun 2005 Jesse says:
    Mitchua, it works for Canadians! I didn't know if it would but your comment showed up in the geocomments view.
  13. Sat, 11 Jun 2005 count de monaco says:
    good job tommy
  14. Sat, 11 Jun 2005 count de monaco says:
    good job tommy
  15. Sat, 11 Jun 2005 ABQ Tester says:
    This is a test. Wow!
  16. Sat, 11 Jun 2005 Sweet says:
    another great g-maps integration!
  17. Sun, 12 Jun 2005 Meh says:
    How cool
  18. Sun, 12 Jun 2005 blackant says:
    Nice.
  19. Sun, 12 Jun 2005 test says:
    test
  20. Mon, 13 Jun 2005 Mike says:
    I like it.
  21. Mon, 13 Jun 2005 Wes says:
    This is great! I'm not sure how practical it is for a blog, but this would be good for sites that have a community built around them.
  22. Mon, 13 Jun 2005 Seth says:
    Hmm.. test!
  23. Mon, 13 Jun 2005 Stephen Caudill says:
    Nice!
  24. Mon, 13 Jun 2005 John Andrews says:
    Cool. I have ideas... wil email!
  25. Mon, 13 Jun 2005 Wesley says:
    Que interesante!
  26. Mon, 13 Jun 2005 Michael Schubert says:
    Test from California :-)
  27. Mon, 13 Jun 2005 Justin Palmer says:
    Great work, it would be interesting to see what comes of this!
  28. Tue, 14 Jun 2005 Robert Bousquet says:
    This is great!
  29. Thu, 16 Jun 2005 hello says:
    oh yeah
  30. Fri, 17 Jun 2005 Brodie says:
    Here I am, brain spinning into orbit with new applications for your coup here.
  31. Sun, 19 Jun 2005 Steve Realton says:
    This looks great
  32. Thu, 23 Jun 2005 Joe says:
    hello
  33. Thu, 23 Jun 2005 Jason Tucker says:
    Awesome!
  34. Tue, 28 Jun 2005 M. Chase Whittemore says:
    I must say, this is one cool feature. I also like your book burro script. In fact most of the things you do on this site, are fun to play around with. So ROCK ON!
  35. Wed, 29 Jun 2005 fabio says:
    nice
  36. Wed, 13 Jul 2005 test says:
    test
  37. Fri, 22 Jul 2005 test says:
    amazing
  38. Mon, 10 Mar 2008 jerik says:
    the googlemaps key does not work anymore and with this the pages small and application view. cheers -- jerik

Leave a response

My Card Add to your Address Book

Jesse Andrews
open source, web browsers, web services, web sites & folk dancing. contacts/sites

Keep Up To Date

Get updates via RSS or
get email when I blog

Previous Blog Posts