rails-geocoder 0.9.3 → 0.9.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.rdoc +4 -0
- data/README.rdoc +5 -15
- data/VERSION +1 -1
- data/lib/geocoder.rb +12 -16
- data/rails-geocoder.gemspec +1 -1
- data/test/fixtures/madison_square_garden.xml +69 -16
- data/test/geocoder_test.rb +2 -2
- metadata +2 -2
data/CHANGELOG.rdoc
CHANGED
data/README.rdoc
CHANGED
@@ -1,10 +1,8 @@
|
|
1
1
|
= Geocoder
|
2
2
|
|
3
|
-
Geocoder adds object geocoding and database-agnostic distance calculations to Ruby on Rails. It's as simple as calling <tt>fetch_coordinates!</tt> on your objects, and then using a scope like <tt>Venue.near("Billings, MT")</tt>.
|
3
|
+
Geocoder adds object geocoding and database-agnostic distance calculations to Ruby on Rails. It's as simple as calling <tt>fetch_coordinates!</tt> on your objects, and then using a scope like <tt>Venue.near("Billings, MT")</tt>. Since it does not rely on proprietary database functions finding geocoded objects in a given area works with out-of-the-box MySQL or even SQLite.
|
4
4
|
|
5
|
-
Geocoder
|
6
|
-
|
7
|
-
<b>Geocoder is currently compatible with Rails 2.x and Rails 3.</b>
|
5
|
+
<b>Geocoder is currently compatible with Rails 2.x and Rails 3.</b> Also, a Google Maps API key is no longer necessary.
|
8
6
|
|
9
7
|
|
10
8
|
== 1. Install
|
@@ -41,21 +39,16 @@ or as a gem:
|
|
41
39
|
|
42
40
|
== 2. Configure
|
43
41
|
|
44
|
-
A)
|
45
|
-
|
46
|
-
# eg, in config/initializers/google_maps.rb
|
47
|
-
GOOGLE_MAPS_API_KEY = "..."
|
48
|
-
|
49
|
-
B) Add +latitude+ and +longitude+ columns to your model:
|
42
|
+
A) Add +latitude+ and +longitude+ columns to your model:
|
50
43
|
|
51
44
|
script/generate migration AddLatitudeAndLongitudeToYourModel latitude:float longitude:float
|
52
45
|
rake db:migrate
|
53
46
|
|
54
|
-
|
47
|
+
B) Tell geocoder where your model stores its address:
|
55
48
|
|
56
49
|
geocoded_by :address
|
57
50
|
|
58
|
-
|
51
|
+
C) Optionally, auto-fetch coordinates every time your model is saved:
|
59
52
|
|
60
53
|
after_validation :fetch_coordinates
|
61
54
|
|
@@ -154,9 +147,6 @@ If anyone has a more elegant solution to this problem I am very interested in se
|
|
154
147
|
* make 'near' scope work with 'select' options
|
155
148
|
* prepend table names to column names in SQL distance expression (required
|
156
149
|
to do joins on another geocoded model)
|
157
|
-
* prevent NameError when GOOGLE_MAPS_API_KEY is missing: show nice msg
|
158
|
-
* <tt>install.rb</tt> should do some setup when installed as a plugin
|
159
|
-
* create initializer with GOOGLE_MAPS_API_KEY?
|
160
150
|
|
161
151
|
|
162
152
|
Copyright (c) 2009-10 Alex Reisner, released under the MIT license
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.9.
|
1
|
+
0.9.4
|
data/lib/geocoder.rb
CHANGED
@@ -181,10 +181,10 @@ module Geocoder
|
|
181
181
|
if options != {}
|
182
182
|
warn "DEPRECATION WARNING: The 'options' argument to the nearbys " +
|
183
183
|
"method is deprecated and will be removed from rails-geocoder in " +
|
184
|
-
"version
|
185
|
-
"more scopes and/or conditions via chaining. For example: " +
|
184
|
+
"a future version. Nearbys now returns a scope so you should " +
|
185
|
+
"specify more scopes and/or conditions via chaining. For example: " +
|
186
186
|
"city.nearbys(20).order('name').limit(10). Support for Rails 2.x " +
|
187
|
-
"will be discontinued
|
187
|
+
"will be discontinued soon."
|
188
188
|
end
|
189
189
|
return [] unless geocoded?
|
190
190
|
options.reverse_merge!(:conditions => ["id != ?", id])
|
@@ -223,15 +223,14 @@ module Geocoder
|
|
223
223
|
return nil unless doc = self.search(query)
|
224
224
|
|
225
225
|
# make sure search found a result
|
226
|
-
e = doc.elements['
|
227
|
-
return nil unless (e and e.text == "
|
228
|
-
|
226
|
+
e = doc.elements['GeocodeResponse/status']
|
227
|
+
return nil unless (e and e.text == "OK")
|
228
|
+
|
229
229
|
# isolate the relevant part of the result
|
230
|
-
place = doc.elements['
|
230
|
+
place = doc.elements['GeocodeResponse/result/geometry/location']
|
231
231
|
|
232
|
-
#
|
233
|
-
|
234
|
-
coords.split(',')[0...2].reverse.map{ |i| i.to_f }
|
232
|
+
# blindly use the first results (assume they are most accurate)
|
233
|
+
['lat', 'lng'].map{ |i| place.elements[i].text.to_f }
|
235
234
|
end
|
236
235
|
|
237
236
|
##
|
@@ -331,13 +330,10 @@ module Geocoder
|
|
331
330
|
#
|
332
331
|
def self._fetch_xml(query)
|
333
332
|
params = {
|
334
|
-
:
|
335
|
-
:
|
336
|
-
:output => "xml",
|
337
|
-
:sensor => "false",
|
338
|
-
:oe => "utf8"
|
333
|
+
:address => query,
|
334
|
+
:sensor => "false"
|
339
335
|
}
|
340
|
-
url
|
336
|
+
url = "http://maps.google.com/maps/api/geocode/xml?" + params.to_query
|
341
337
|
|
342
338
|
# Query geocoder and make sure it responds quickly.
|
343
339
|
begin
|
data/rails-geocoder.gemspec
CHANGED
@@ -1,16 +1,69 @@
|
|
1
|
-
|
2
|
-
<
|
3
|
-
|
4
|
-
<
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
</
|
16
|
-
|
1
|
+
<GeocodeResponse>
|
2
|
+
<status>OK</status>
|
3
|
+
<result>
|
4
|
+
<type>street_address</type>
|
5
|
+
<formatted_address>4 Penn Plaza, New York, NY 10001, USA</formatted_address>
|
6
|
+
<address_component>
|
7
|
+
<long_name>4</long_name>
|
8
|
+
<short_name>4</short_name>
|
9
|
+
<type>street_number</type>
|
10
|
+
</address_component>
|
11
|
+
<address_component>
|
12
|
+
<long_name>Penn Plaza</long_name>
|
13
|
+
<short_name>Penn Plaza</short_name>
|
14
|
+
<type>route</type>
|
15
|
+
</address_component>
|
16
|
+
<address_component>
|
17
|
+
<long_name>Manhattan</long_name>
|
18
|
+
<short_name>Manhattan</short_name>
|
19
|
+
<type>sublocality</type>
|
20
|
+
<type>political</type>
|
21
|
+
</address_component>
|
22
|
+
<address_component>
|
23
|
+
<long_name>New York</long_name>
|
24
|
+
<short_name>New York</short_name>
|
25
|
+
<type>locality</type>
|
26
|
+
<type>political</type>
|
27
|
+
</address_component>
|
28
|
+
<address_component>
|
29
|
+
<long_name>New York</long_name>
|
30
|
+
<short_name>New York</short_name>
|
31
|
+
<type>administrative_area_level_2</type>
|
32
|
+
<type>political</type>
|
33
|
+
</address_component>
|
34
|
+
<address_component>
|
35
|
+
<long_name>New York</long_name>
|
36
|
+
<short_name>NY</short_name>
|
37
|
+
<type>administrative_area_level_1</type>
|
38
|
+
<type>political</type>
|
39
|
+
</address_component>
|
40
|
+
<address_component>
|
41
|
+
<long_name>United States</long_name>
|
42
|
+
<short_name>US</short_name>
|
43
|
+
<type>country</type>
|
44
|
+
<type>political</type>
|
45
|
+
</address_component>
|
46
|
+
<address_component>
|
47
|
+
<long_name>10001</long_name>
|
48
|
+
<short_name>10001</short_name>
|
49
|
+
<type>postal_code</type>
|
50
|
+
</address_component>
|
51
|
+
<geometry>
|
52
|
+
<location>
|
53
|
+
<lat>40.7503540</lat>
|
54
|
+
<lng>-73.9933710</lng>
|
55
|
+
</location>
|
56
|
+
<location_type>ROOFTOP</location_type>
|
57
|
+
<viewport>
|
58
|
+
<southwest>
|
59
|
+
<lat>40.7473324</lat>
|
60
|
+
<lng>-73.9965316</lng>
|
61
|
+
</southwest>
|
62
|
+
<northeast>
|
63
|
+
<lat>40.7536276</lat>
|
64
|
+
<lng>-73.9902364</lng>
|
65
|
+
</northeast>
|
66
|
+
</viewport>
|
67
|
+
</geometry>
|
68
|
+
</result>
|
69
|
+
</GeocodeResponse>
|
data/test/geocoder_test.rb
CHANGED
@@ -4,8 +4,8 @@ class GeocoderTest < Test::Unit::TestCase
|
|
4
4
|
|
5
5
|
def test_fetch_coordinates
|
6
6
|
v = Venue.new(*venue_params(:msg))
|
7
|
-
assert_equal [40.
|
8
|
-
assert_equal [40.
|
7
|
+
assert_equal [40.750354, -73.993371], v.fetch_coordinates
|
8
|
+
assert_equal [40.750354, -73.993371], [v.latitude, v.longitude]
|
9
9
|
end
|
10
10
|
|
11
11
|
# sanity check
|