January 23, 2021
Hot Topics:

Geocoding with the Rails GeoKit Plugin

  • By W. Jason Gilmore
  • Send Email »
  • More Articles »
def distance

   # Retrieve the coordinate for Mitchell's Steakhouse
   mitchells = GoogleGeocoder.geocode("45 North Third Street,
                                       Columbus, Ohio 43215")

   # Retrieve the coordinates for the art museum
   museum = GoogleGeocoder.geocode("480 E Broad St, Columbus,
                                    Ohio 43215")

   # Retrieve the coordinates for Columbus, Ohio
   # (for map centering purposes)
   city = GoogleGeocoder.geocode("Columbus, Ohio")

   # Calculate the distance between the two locations
   @distance = mitchells.distance_from(museum)

   # Create a new map and center it on Columbus
   @map = GMap.new("map")
   @map.center_zoom_init([city.lat, city.lng], 14)

   # Add an icon denoting Mitchell's location
   mitchells_marker = GMarker.new([mitchells.lat,mitchells.lng])

   # Add an icon denoting the museum's location
   museum_marker = GMarker.new([museum.lat,museum.lng])


The relevant part of the distance.html.erb view is shown next:

   The distance between Mitchell's Steakhouse and the Columbus
   Art Museum is <%= @distance.round(2) %> miles.
<%= @map.div(:width => 400, :height => 300) %>

Executing this action produces the outcome shown in Figure 2.

Click here for a larger image.

Figure 2: Calculating the difference between two points

Finding All Points Within a Specified Radius

Continuing your tour of GeoKit's fascinating distance calculation features, suppose your pizzeria directory consisted of numerous pizzerias strewn throughout the Ohio region, but the user only wanted to see pizzerias located within 25 miles of a specific location. Doing so with GeoKit is laughably easy, thanks to a set of finder methods. For instance, to identify all pizzerias located within 25 miles of the Youngstown, Ohio area, you would execute the following code within your controller:

city = GoogleGeocoder.geocode("Youngstown, Ohio")
pizzerias = Pizzeria.find(:all, :origin => [city.lat, city.lng],
   :within => 25)

From here, you can cycle through each pizzeria, adding its icon to the map. Combining this approach with what you learned in the previous installment, the user might encounter four pizzerias in the Youngstown area:

Figure 3: Finding all points within a specific radius

Identifying User Locations

Many mapping applications are intended to help the user take advantage of the service in the context of his current location. Even though you could explicitly ask the user for his location, wouldn't it be much cooler if you could automatically derive his location and adjust the map accordingly? Using GeoKit's IP geocoding feature, you can do this simply by obtaining the user's IP address.

GeoKit accomplishes this seemingly magical feat by passing the user's IP address to the Hostip.info API, which will look up the address in its database and return the user's city and state if found.

Page 2 of 3

This article was originally published on July 21, 2008

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date