developer.com
Search EarthWeb
CodeGuru | Gamelan | Jars | Wireless | Discussions
Navigate developer.com
Architecture & Design  
Database  
Java
Languages & Tools
Microsoft & .NET
Open Source  
Project Management  
Security  
Techniques  
Voice  
Web Services  
Wireless/Mobile
XML  
New
 
Technology Jobs  

   Developer.com Webcasts:
  The Impact of Coding Standards and Code Reviews

  Project Management for the Developer

  Defining Your Own Software Development Methodology

  more Webcasts...




See the Winners!




Developer Jobs

Be a Commerce Partner














 


Developer News -
Mozilla's Ubquity Mashup: For The Masses?    August 27, 2008
iPhone Users Just Want to Have Fun    August 26, 2008
Oops! I Fixed the Linux Kernel    August 22, 2008
Jim Zemlin: The New Center of Linux Gravity    August 21, 2008
Free Tech Newsletter -

Adding Google Maps To Your Rails Applications
By Jason Gilmore

Go to page: 1  2  3  Next  

In the months following publication of the final part of the very popular series on integrating Google Maps into PHP applications, I've spent quite a bit of time working with another popular Web technology: Ruby on Rails. As it turns out, Rails developers have been hard at work creating a few amazing plugins capable of adding powerful mapping capabilities to your applications. In this new series, I'll introduce you to these powerful plugins, showing you a number of tips and tricks along the way.

I'll presume you're familiar with mapping fundamentals, including the basic ideas surrounding the Google mapping API syntax. If you haven't had the opportunity to experiment with the API, take some time to read this tutorial before continuing.

Introducing the YM4R/GM Plugin

Although there's nothing preventing you from linking to Google's mapping JavaScript API and referencing the library directly from your views, jumping between Ruby/Rails syntax and JavaScript can quickly become a tedious affair. The YM4R/GM plugin remedies this issue nicely, abstracting the API calls through Ruby's familiar object-oriented syntax. With it you can do everything from render simple maps to build complex maps complete with custom markers, information windows, and clusters for facilitating the rendering of large numbers of markers.

Installing and Configuring YM4R/GM

To install the YM4R/GM plugin, execute the following command from your project directory:

%>ruby script/plugin install
   svn://rubyforge.org/var/svn/ym4r/Plugins/GM/trunk/ym4r_gm

YM4R/GM manages the Google API keys within a file named gmaps_api_key.yml, found in the project's config directory. The developers save you the trouble of having to create your own API key for local testing purposes by including an API key that has already been tied to http://localhost:3000. However, if you're testing on a different host, you'll first need to create an API key and add it to this file (instructions for creating a key are provided in the aforementioned introductory tutorial).

Creating Your First Map

To get acquainted with YM4R/GM's syntax, you can begin by creating the map displayed in Figure 1.

Figure 1: Centering the map over Youngstown, Ohio

As is standard Rails practice, you'll use the controller method to define the map and its features, and the view to render the results. In the following example, you'll define a map in the index controller's index action, complete with a pan/zoom control but minus the map type selector:

def index
   # Create a new map object, also defining the div ("map") 
   # where the map will be rendered in the view
   @map = GMap.new("map")
   # Use the larger pan/zoom control but disable the map type
   # selector
   @map.control_init(:large_map => true,:map_type => false)
   # Center the map on specific coordinates and focus in fairly
   # closely
   @map.center_zoom_init([41.023849,-80.682053], 10)
end

Next, in the index action's corresponding view, add the following code:

<html>
   <head>
      <title>Test</title>
      <%= GMap.header %>
      <%= @map.to_html %>
   </head>
   <body>
      <%= @map.div(:width => 400, :height => 300) %>
   </body>
</html>

The GMap.header call will output references to both the Google Maps API and YM4R/GM JavaScript libraries. The @map.to_html call outputs JavaScript code generated by YM4R/GM according to the specifications set forth in the action. Finally, the @map.div call outputs the map to a div as specified in the action's GMap.new call.

Also, you'll see that the map dimensions are defined in the view rather than the controller. This is keeping with the convention of separating application logic and design; the view designer can choose any dimension he pleases; the map will simply fill to the desired size. The initial zoom level is, however, defined in the controller, although the user can easily subsequently adjust the zoom using the control.

Go to page: 1  2  3  Next  

Next article: Geocoding with the Rails GeoKit Plugin


Tools:
Add www.developer.com to your favorites
Add www.developer.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed


Ruby / Rails Archives








JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers