geomap 0.0.9 → 0.0.10
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/lib/geomap.rb +39 -30
- metadata +1 -1
data/lib/geomap.rb
CHANGED
@@ -6,47 +6,56 @@ require 'active_record'
|
|
6
6
|
class Geomap
|
7
7
|
|
8
8
|
attr_accessor :latitude, :longitude, :address
|
9
|
+
|
9
10
|
class << self
|
10
|
-
|
11
|
-
|
12
|
-
|
11
|
+
|
12
|
+
def reverse_geocode lat, lng
|
13
|
+
get_results [lat, lng]
|
14
|
+
end
|
13
15
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
16
|
+
def geocode address
|
17
|
+
address = address.gsub(/\s/, '+')
|
18
|
+
get_results [address]
|
19
|
+
end
|
18
20
|
|
19
|
-
|
20
|
-
def get_results args, cond
|
21
|
+
def get_results args
|
21
22
|
|
22
|
-
|
23
|
-
|
23
|
+
geocoder_url = "http://maps.googleapis.com/maps/api/geocode/json?"
|
24
|
+
params = ""
|
24
25
|
|
25
|
-
|
26
|
+
params = args.size == 2 ? "latlng=#{args[0]},#{args[1]}" : "address=#{args[0]}"
|
26
27
|
|
27
|
-
|
28
|
+
url ="#{geocoder_url}#{params}&sensor=true"
|
28
29
|
|
29
|
-
|
30
|
-
|
30
|
+
resp = Net::HTTP.get_response(URI.parse(url))
|
31
|
+
data = resp.body
|
31
32
|
|
32
|
-
|
33
|
+
result = JSON.parse(data)
|
33
34
|
|
34
|
-
|
35
|
-
|
36
|
-
|
35
|
+
if result.nil?
|
36
|
+
raise "Error --- invalid input"
|
37
|
+
end
|
37
38
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
if cond
|
44
|
-
result ["results"].first["address_components"].first["long_name"]
|
45
|
-
else
|
46
|
-
[result["results"].first["geometry"]["location"]["lat"], result["results"].first["geometry"]["location"]["lng"]]
|
47
|
-
end
|
39
|
+
if result.has_key? 'Error'
|
40
|
+
raise "web service error"
|
41
|
+
end
|
48
42
|
|
49
|
-
|
43
|
+
|
44
|
+
puts result
|
45
|
+
|
46
|
+
|
47
|
+
if result["status"] == "OK"
|
48
|
+
if args.size == 2
|
49
|
+
result["results"].first["address_components"].first["long_name"]
|
50
|
+
else
|
51
|
+
[result["results"].first["geometry"]["location"]["lat"], result["results"].first["geometry"]["location"]["lng"]]
|
52
|
+
|
53
|
+
end
|
54
|
+
else
|
55
|
+
return args.size == 2 ? "" : [0, 0]
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
50
59
|
end
|
51
60
|
end
|
52
61
|
|