Pages

Friday, July 16, 2010

Google Map in Rails

To have google map in an application.First we have to get a key from this link
http://code.google.com/apis/maps/signup.html
Signing up a key for http://yourdomain.com is usually the best practice, as it will work for all sub domains and directories.

Steps to include Google Map in an application

1. Generate an application geomap with latitude:decimal ,longitude:decimal,location
name:string and place description:string using scaffold.

2. We have to show this data in the map.Map we will get from the google server.

3. Download the file at http://tinyurl.com/hfrailsmap .Its a partial file save it as _map.html.erb and place in the app/views/geomaps/ like app/views/geomaps/_map.html.erb

4. We have the update the google_key in the _map.html.erb file with the key we got from google api link.

5. Copy the generated code from the show.html.erb & paste in new file called
show_map.html.erb.place it in app/view/geomaps/show_map.html.erb

6. In the app\controllers\geomaps_controller.rb place the following code

def show_map
@geomap=Geomap.find(params[:id])
end


7. Include the following code in the route.rb located in config folder.
map.connect 'geomaps/map/:id',:action=>'show_map',:controller=>'geomaps'


8. Place a test.xml data file in the public folder.This to check the map with static data.
 <data>
  <description>Vision Tech Solutions Description: Vision Tech Solutions is a Software Product and Outsourced Software Product Development firm, dedicated to provide custom made software services to customer's need of any size within global reach.</description>
  <latitude>10.784745 </latitude>
  <longitude>78.689575 </longitude>
  <title>Vision Tech Solutions</title>
 </data>


9. We have to now include the _map.html.erb partial code in the display page show_map.html.erb
<%=render(:partial=>'map',:local=>{:data=>'test.xml'}) %>

We need to pass a local variable called data containing the path to
the map data /test.xml

10. Check the display of map in http://yourdomain:3000/geomaps/map/1

No comments:

Post a Comment