You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.rdoc
+117-57
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ Geocoder is a complete geocoding solution for Ruby. With Rails it adds geocoding
8
8
* Supports multiple Ruby versions: Ruby 1.8.7, 1.9.2, and JRuby.
9
9
* Supports multiple databases: MySQL, PostgreSQL, SQLite, and MongoDB (1.7.0 and higher).
10
10
* Supports Rails 3. If you need to use it with Rails 2 please see the <tt>rails2</tt> branch (no longer maintained, limited feature set).
11
-
* Works very well outside of Rails but you'll need to install either the +json+ (for MRI) or +json_pure+ (for JRuby) gem.
11
+
* Works very well outside of Rails, you just need to install either the +json+ (for MRI) or +json_pure+ (for JRuby) gem.
12
12
13
13
14
14
== Install
@@ -208,73 +208,121 @@ Every <tt>Geocoder::Result</tt> object, +result+, provides the following data:
208
208
* <tt>result.coordinates</tt> - array of the above two
209
209
* <tt>result.address</tt> - string
210
210
* <tt>result.city</tt> - string
211
+
* <tt>result.state</tt> - string
212
+
* <tt>result.state_code</tt> - string
211
213
* <tt>result.postal_code</tt> - string
212
214
* <tt>result.country_name</tt> - string
213
215
* <tt>result.country_code</tt> - string
214
216
215
-
and if you're familiar with the results returned by the geocoding service you're using, you can access even more (see code comments for details: <tt>lib/geocoder/results/*</tt>).
217
+
If you're familiar with the results returned by the geocoding service you're using you can access even more data, but you'll need to be familiar with the particular <tt>Geocoder::Result</tt> object you're using and the structure of your geocoding service's responses. (See below for links to geocoding service documentation.)
216
218
217
219
218
220
== Geocoding Services
219
221
220
-
By default Geocoder uses Google's geocoding API to fetch coordinates and addresses. However if you wish to use Yahoo's geocoding API you can simply add this to an initializer:
222
+
By default Geocoder uses Google's geocoding API to fetch coordinates and street addresses (FreeGeoIP is used for IP address info). However there are several other APIs supported, as well as a variety of settings. Please see the listing and comparison below for details on specific geocoding services (not all settings are supported by all services). The configuration options are:
221
223
222
224
# config/initializers/geocoder.rb
223
-
Geocoder::Configuration.lookup = :yahoo
224
-
225
-
Street address geocoding services currently supported (valid settings for the above):
226
-
227
-
* Google: <tt>:google</tt>
228
-
* Yahoo: <tt>:yahoo</tt>
229
-
* Bing: <tt>:bing</tt>
230
-
* Geocoder.ca: <tt>:geocoder_ca</tt> (US and Canada only)
231
-
232
-
Note that the result objects returned by different geocoding services all implement the methods listed above. Beyond that, however, you must be familiar with your particular subclass of <tt>Geocoder::Result</tt> and the geocoding service's result structure:
You can set the timeout used for connections to the geocoding service. The default is 3 seconds but if you want to set it to 5, for example, put the following in an initializer:
257
-
232
+
# geocoding service request timeout, in seconds (default 3):
258
233
Geocoder::Configuration.timeout = 5
259
234
260
-
=== Language
261
-
262
-
You can set the language used for reverse geocoding results to German, for example, by setting the following:
235
+
# use HTTPS for geocoding service connections:
236
+
Geocoder::Configuration.use_https = true
263
237
238
+
# language to use (for search queries and reverse geocoding):
264
239
Geocoder::Configuration.language = :de
265
240
266
-
For a list of supported languages see the documentation for the geocoding service you're using.
267
-
268
-
=== HTTPS
241
+
# use a proxy to access the service:
242
+
Geocoder::Configuration.http_proxy = "127.4.4.1"
243
+
Geocoder::Configuration.https_proxy = "127.4.4.2" # only if HTTPS is needed
269
244
270
-
If you want to use HTTPS for geocoding service connections:
271
-
272
-
Geocoder::Configuration.use_https = true
273
-
274
-
Note that currently the only service that supports HTTPS is Google.
245
+
# caching (see below for details)
246
+
Geocoder::Configuration.cache = Redis.new
247
+
Geocoder::Configuration.cache_prefix = "..."
275
248
276
249
277
-
== Caching Results
250
+
=== Listing and Comparison
251
+
252
+
The following is a comparison of the supported geocoding APIs. The "Limitations" listed for each are a very brief and incomplete summary of some special limitations beyond basic data source attribution. Please read the official Terms of Service for a service before using it.
Terms of Service:: http://code.google.com/apis/maps/terms.html#section_10_12
264
+
Limitations:: "You must not use or display the Content without a corresponding Google map, unless you are explicitly permitted to do so in the Maps APIs Documentation, or through written permission from Google." "You must not pre-fetch, cache, or store any Content, except that you may store: (i) limited amounts of Content for the purpose of improving the performance of your Maps API Implementation..."
265
+
266
+
==== Yahoo (<tt>:yahoo</tt>)
267
+
268
+
API key:: optional in development (required for production apps)
Terms of Service:: http://info.yahoo.com/legal/us/yahoo/maps/mapsapi/mapsapi-2141.html
276
+
Limitations:: "YOU SHALL NOT... (viii) store or allow end users to store map imagery, map data or geocoded location information from the Yahoo! Maps APIs for any future use; (ix) use the stand-alone geocoder for any use other than displaying Yahoo! Maps or displaying points on Yahoo! Maps;"
Terms of Service:: http://www.microsoft.com/maps/product/terms.html
288
+
Limitations:: No country codes or state names. Must be used on "public-facing, non-password protected web sites," "in conjunction with Bing Maps or an application that integrates Bing Maps."
It's a good idea, when relying on any external service, to cache retrieved data. When implemented correctly it improves your app's response time and stability. It's easy to cache geocoding results with Geocoder, just configure a cache store:
280
328
@@ -301,6 +349,8 @@ If you need to expire cached content:
301
349
302
350
Do *not* include the prefix when passing a URL to be expired. Expiring <tt>:all</tt> will only expire keys with the configured prefix (won't kill every entry in your key/value store).
303
351
352
+
<i>Before you implement caching in your app please be sure that doing so does not violate the Terms of Service for your geocoding service.</i>
353
+
304
354
305
355
== Forward and Reverse Geocoding in the Same Model
306
356
@@ -343,6 +393,23 @@ You can use Geocoder outside of Rails by calling the <tt>Geocoder.search</tt> me
343
393
This returns an array of <tt>Geocoder::Result</tt> objects with all information provided by the geocoding service. Please see above and in the code for details.
344
394
345
395
396
+
== Command Line Interface
397
+
398
+
When you install the Geocoder gem it adds a +geocode+ command to your shell. You can search for a street address, IP address, postal code, coordinates, etc just like you can with the Geocoder.search method for example:
399
+
400
+
$ geocode 29.951,-90.081
401
+
Latitude: 29.952211
402
+
Longitude: -90.080563
403
+
Full address: 1500 Sugar Bowl Dr, New Orleans, LA 70112, USA
404
+
City: New Orleans
405
+
State/province: Louisiana
406
+
Postal code: 70112
407
+
Country: United States
408
+
Google map: http://maps.google.com/maps?q=29.952211,-90.080563
409
+
410
+
There are also a number of options for setting the geocoding API, key, and language, viewing the raw JSON reponse, and more. Please run <tt>geocode -h</tt> for details.
411
+
412
+
346
413
== Notes on Mongoid
347
414
348
415
=== The Near Method
@@ -351,7 +418,7 @@ Mongoid document classes have a built-in +near+ scope, but since it only works t
351
418
352
419
=== Latitude/Longitude Order
353
420
354
-
Coordinates are generally printed and spoken as latitude, then logitude ([lat,lon]). Geocoder respects this convention and always expects method arguments to be given in [lat,lon] order. However, MongoDB requires that coordinates be stored in [lon,lat] order as per the GeoJSON spec (http://geojson.org/geojson-spec.html#positions), so internally they are stored "backwards." However, this does not affect order of arguments to methods when using Mongoid. I mention this only in case you notice it and freak out. Don't worry. Everything is going to be OK.
421
+
Coordinates are generally printed and spoken as latitude, then logitude ([lat,lon]). Geocoder respects this convention and always expects method arguments to be given in [lat,lon] order. However, MongoDB requires that coordinates be stored in [lon,lat] order as per the GeoJSON spec (http://geojson.org/geojson-spec.html#positions), so internally they are stored "backwards." However, this does not affect order of arguments to methods when using Mongoid.
You cannot use the +near+ scope with another scope that provides an +includes+ option because the +SELECT+ clause generated by +near+ will overwrite it (or vice versa). Instead, try using +joins+ and pass a <tt>:select</tt> option to the +near+ scope to get the columns you want. For example, in Rails 2 syntax:
451
+
You cannot use the +near+ scope with another scope that provides an +includes+ option because the +SELECT+ clause generated by +near+ will overwrite it (or vice versa). Instead, try using +joins+ and pass a <tt>:select</tt> option to the +near+ scope to get the columns you want. For example:
Copy file name to clipboardexpand all lines: geocoder.gemspec
+1-1
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,7 @@ Gem::Specification.new do |s|
11
11
s.homepage="http://www.rubygeocoder.com"
12
12
s.date=Date.today.to_s
13
13
s.summary="Complete geocoding solution for Ruby."
14
-
s.description="Provides object geocoding (by street or IP address), reverse geocoding (coordinates to street address), and distance queries for ActiveRecord and Mongoid. Designed for Rails but works with other Rack frameworks too."
14
+
s.description="Provides object geocoding (by street or IP address), reverse geocoding (coordinates to street address), distance queries for ActiveRecord and Mongoid, result caching, and more. Designed for Rails but works with Sinatra and other Rack frameworks too."
0 commit comments