This post originated from an RSS feed registered with Ruby Buzz
by Warren Brian Noronha.
Original Post: Google maps on Rails
Feed Title: HyperionReactor - Ruby, Ruby On Rails
Feed URL: http://viper.unixbsd.info/suspended.page/
Feed Description: My blogs on Ruby and Ruby on Rails.
I had to build a complex event management and tracking system. The old developer simply provided a link to Webmaps. I really did not like this approach, and decided to check out Google and Yahoo maps. So I signed up for a Google and Yahoo Developer Id.
I started off with YahooMaps, since I have a lot of friends at Yahoo, I knew it would be easy to get help from them if I get stuck somewhere down the road. After a quick read through their documentation I found out:
They support REST which was good.
I can directly send them the address and they would display the correct map
You can't embed a YahooMap on your homepage
Good part is they allow you to load it into a iframe.
They also have this really cool YahooMap Live Traffic Update, which is cool in its own way...
I then moved on to GoogleMaps. We all know how cool they are :) Google seems easy to implement. Not as easy as Yahoo though. So this is what I found out about Google:
You can embed the image in a page ( Yay! )
You can add your own custom layers
The really cool animation and ajax
They don't support REST
You have to send them the latitude and longitude
You have to use a third party service to convert your address to latitude and longitude so GoogleMaps could generate the map.
GoogleMaps won hands down, mainly cause of their ability to embed the map on your page.
Implementation
You will first need to signup for a GoogleDeveloper Id. After you get the developer id; you need to get the key to embed the map on your homepage. These keys work only for a specific urls e.g. http://www.hyperionractor.net/maps/ so now the maps will work on all the pages under '/maps' can will work but not under '/'.
You will need to signup for a geocoder service now. This is because you will need to convert your address to a latitude and longitude. I am using geocoder.us/ it is a free service; but I suggest you make a donation or a subscribe to their service if your are using it for commercial purposes. They support XML-RPC, REST and SOAP. I picked XML-RPC.
Controller code (sample)
def view
event = Event.find(params[:id])
server = XMLRPC::Client.new2('http://rpc.geocoder.us/service/xmlrpc')
result = server.call2('geocode', event.address)
@lat = result[1][0]['lat']
@long = result[1][0]['long']
end
It is important that you create a onload() function, this is because IE has issues if the map is generated before the page is loaded or something like that. What we are going to do is load the entire page, and then load the map. In this way the map will work across all browsers. If you dont you should keep getting a "Operation Aborted" error.
This goes at the bottom of your page:
<% content_for('head_t') do %>
<% end %>
This will automatically be added to the head tag. You have to replace my_key with your Google Key. and every thing else stays the same.
<% content_for('body_t') do %>onload='onload()'<% end %>
This will add the onload='onload()' to your body tag. This will tell the browser to start loading the map, only after the entire page has loaded.
You can also create some kind of GoogleMap Helper if you are using a lot of maps. If you have found a better way to use GoogleMaps and YahooMaps please do leave a comment.