Module: Gmaps4rails
- Defined in:
- lib/gmaps4rails.rb,
lib/acts_as_gmappable/base.rb,
app/helpers/gmaps4rails/gmaps_helper.rb,
app/controllers/gmaps4rails/gmaps_controller.rb,
lib/rails/generators/gmaps4rails/templates/initializer.rb
Defined Under Namespace
Modules: ActsAsGmappable, GmapsHelper
Classes: DirectionInvalidQuery, DirectionNetStatus, DirectionStatus, Engine, GeocodeInvalidQuery, GeocodeNetStatus, GeocodeStatus, GmapsController
Class Method Summary
collapse
Class Method Details
.create_json(object) ⇒ Object
15
16
17
18
19
20
21
22
|
# File 'lib/acts_as_gmappable/base.rb', line 15
def Gmaps4rails.create_json(object)
unless object[object.gmaps4rails_options[:lat_column]].blank? && object[object.gmaps4rails_options[:lng_column]].blank?
"{
\"description\": \"#{object.gmaps4rails_infowindow}\", \"title\": \"#{object.gmaps4rails_title}\", \"sidebar\": \"#{object.}\",
\"longitude\": \"#{object[object.gmaps4rails_options[:lng_column]]}\", \"latitude\": \"#{object[object.gmaps4rails_options[:lat_column]]}\", \"picture\": \"#{object.gmaps4rails_marker_picture['picture']}\", \"width\": \"#{object.gmaps4rails_marker_picture['width']}\", \"height\": \"#{object.gmaps4rails_marker_picture['height']}\"
} ,"
end
end
|
.destination(start_end, options = {}, output = "pretty") ⇒ Object
output could be raw, pretty or clean
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/acts_as_gmappable/base.rb', line 65
def Gmaps4rails.destination(start_end, options={}, output="pretty")
if start_end["from"].nil? || start_end["to"].empty?
raise Gmaps4rails::DirectionInvalidQuery, "Origin and destination must be provided in a hash as first argument"
else geocoder = "http://maps.googleapis.com/maps/api/directions/json?origin=#{start_end["from"]}&destination=#{start_end["to"]}"
dest_options = options.empty? ? "" : "&" + options.map {|k,v| v.is_a?(Array) ? k + "=" + v * ("|") : k + "=" + v }*("&")
request = geocoder + dest_options + "&sensor=false"
url = URI.escape(request)
resp = Net::HTTP.get_response(URI.parse(url))
if resp.is_a?(Net::HTTPSuccess)
parse = Crack::JSON.parse(resp.body)
if parse["status"] == "OK"
legs = []
parse["routes"].first["legs"].each do |leg|
polylines = leg["steps"].map {|step| step.delete("polyline")} if output == "pretty" || output == "clean"
legs << {
"duration" => { "text" => leg["duration"]["text"], "value" => leg["duration"]["value"].to_f },
"distance" => { "text" => leg["distance"]["text"], "value" => leg["distance"]["value"].to_f },
"steps" => leg["steps"]
}
if output == "pretty"
polylines.map{|poly| poly.delete("levels")}
json = polylines.map { |poly| {"coded_array" => poly["points"]} }.to_json
legs.last.merge!({ "polylines" => json })
end
end
return legs
else raise Gmaps4rails::DirectionStatus, "The query you passed seems invalid, status was: #{parse["status"]}.
Request was: #{request}"
end else raise Gmaps4rails::DirectionNetStatus, "The request sent to google was invalid (not http success): #{request}.
Response was: #{resp}"
end end end
|
.filter(data) ⇒ Object
114
115
116
117
|
# File 'lib/acts_as_gmappable/base.rb', line 114
def Gmaps4rails.filter(data)
return data if data.is_a?(Numeric) || data.is_a?(TrueClass) || data.is_a?(FalseClass)
"'#{data}'"
end
|
.geocode(address, raw = false) ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/acts_as_gmappable/base.rb', line 24
def Gmaps4rails.geocode(address, raw = false)
if address.nil? || address.empty?
raise Gmaps4rails::GeocodeInvalidQuery, "You must provide an address"
else geocoder = "http://maps.googleapis.com/maps/api/geocode/json?address="
output = "&sensor=false"
request = geocoder + address + output
url = URI.escape(request)
resp = Net::HTTP.get_response(URI.parse(url))
if resp.is_a?(Net::HTTPSuccess)
parse = Crack::JSON.parse(resp.body)
if parse["status"] == "OK"
return parse if raw == true
array = []
parse["results"].each do |result|
array << {
:lat => result["geometry"]["location"]["lat"],
:lng => result["geometry"]["location"]["lng"],
:matched_address => result["formatted_address"],
:bounds => result["geometry"]["bounds"]
}
end
return array
else raise Gmaps4rails::GeocodeStatus, "The address you passed seems invalid, status was: #{parse["status"]}.
Request was: #{request}"
end
else raise Gmaps4rails::GeocodeNetStatus, "The request sent to google was invalid (not http success): #{request}.
Response was: #{resp}"
end end end
|