google_map 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in google_map.gemspec
4
+ gemspec
data/MIT_LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2011
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,180 @@
1
+ ======================================================
2
+ GoogleMap
3
+ ======================================================
4
+ A RubyGem that and makes generating google maps easy as pie.
5
+
6
+
7
+ ======================================================
8
+ INSTALL
9
+ ======================================================
10
+ gem install google_map
11
+
12
+
13
+ ======================================================
14
+ PLUGIN CONFIGURATION
15
+ ======================================================
16
+ 1 - Set your Google Maps API Key in environment.rb (or somewhere else if you'd prefer)
17
+ I'd suggest copying the configuration code out of your environment.rb and into an initializer named geokit
18
+
19
+ # This key is good for localhost:3000, signup for more at http://www.google.com/apis/maps/signup.html
20
+ GOOGLE_APPLICATION_ID = "ABQIAAAA3HdfrnxFAPWyY-aiJUxmqRTJQa0g3IQ9GZqIMmInSLzwtGDKaBQ0KYLwBEKSM7F9gCevcsIf6WPuIQ"
21
+
22
+
23
+
24
+ ======================================================
25
+ MAP CONTROLS
26
+ ======================================================
27
+
28
+ maps_controller.rb
29
+ --------------------------
30
+ class MapsController < ApplicationController
31
+ def show
32
+ @map = GoogleMap::Map.new
33
+
34
+ # define control types shown on map
35
+ @map.controls = [ :large, :scale, :type ]
36
+ # valid controls options include
37
+ # :large
38
+ # :small
39
+ # :overview
40
+ # :large_3d
41
+ # :scale
42
+ # :type
43
+ # :menu_type
44
+ # :hierachical_type
45
+ # :zoom
46
+ # :zoom_3d
47
+ # :nav_label
48
+
49
+ # allow user to double click to zoom
50
+ @map.double_click_zoom = true
51
+
52
+ # not certain what this does
53
+ @map.continuous_zoom = false
54
+
55
+ # allow user to scroll using mouse wheel?
56
+ @map.scroll_wheel_zoom = false
57
+
58
+ end
59
+ end
60
+
61
+ ======================================================
62
+ MAP CENTERING AND ZOOM
63
+ ======================================================
64
+
65
+ maps_controller.rb
66
+ --------------------------
67
+ class MapsController < ApplicationController
68
+ def show
69
+ @map = GoogleMap::Map.new
70
+ @map.center = GoogleMap::Point.new(47.6597, -122.318) #SEATTLE WASHINGTON
71
+ @map.zoom = 10 #200km
72
+ end
73
+ end
74
+
75
+
76
+ ======================================================
77
+ MAP CENTERING USING BOUNDS
78
+ ======================================================
79
+
80
+ maps_controller.rb
81
+ --------------------------
82
+ class MapsController < ApplicationController
83
+ def show
84
+ @map = GoogleMap::Map.new
85
+ @map.bounds = [GoogleMap::Point.new(47.6597, -121.318), GoogleMap::Point.new(48.6597, -123.318)] #SEATTLE WASHINGTON 50KM
86
+ end
87
+ end
88
+
89
+
90
+
91
+ ======================================================
92
+ SIMPLE MARKER USAGE
93
+ ======================================================
94
+
95
+ maps_controller.rb
96
+ --------------------------
97
+ class MapsController < ApplicationController
98
+ def show
99
+ @map = GoogleMap::Map.new
100
+ @map.markers << GoogleMap::Marker.new( :map => @map,
101
+ :lat => 47.6597,
102
+ :lng => -122.318,
103
+ :html => 'My House')
104
+ end
105
+ end
106
+
107
+ maps/show.html.erb
108
+ -------------------------
109
+ <%= @map.to_html %>
110
+ <div style="width: 500px; height: 500px;">
111
+ <%= @map.div %>
112
+ </div>
113
+
114
+
115
+ ======================================================
116
+ Advanced Marker Usage
117
+ ======================================================
118
+
119
+ # Available icon classes:
120
+ # GoogleMap::LetterIcon.new(@map, 'A') # letter must be uppercase
121
+ # GoogleMap::SmallIcon.new(@map, 'yellow')
122
+
123
+ maps_controller.rb
124
+ --------------------------
125
+ class MapsController < ApplicationController
126
+ def show
127
+ @map = GoogleMap::Map.new
128
+ @map.markers << GoogleMap::Marker.new( :map => @map,
129
+ :icon => GoogleMap::SmallIcon.new(@map, 'blue'),
130
+ :lat => 47.6597,
131
+ :lng => -122.318,
132
+ :html => 'My House',
133
+ :marker_icon_path => '/path/to/image',
134
+ :marker_hover_text => 'String to show on Mouse Over',
135
+ :open_infoWindow => true #opens marker by default
136
+ )
137
+
138
+ end
139
+ end
140
+
141
+ maps/show.html.erb
142
+ -------------------------
143
+ <%= @map.to_html %>
144
+ <div style="width: 500px; height: 500px;">
145
+ <%= @map.div %>
146
+ </div>
147
+
148
+
149
+ ======================================================
150
+ PLOTTING POLYLINE ROUTES
151
+ ======================================================
152
+
153
+ maps_controller.rb
154
+ --------------------------
155
+ class MapsController < ApplicationController
156
+ def show
157
+ @map = GoogleMap::Map.new
158
+
159
+ # plot points for polyline
160
+ vertices = []
161
+ object.gpxroute.gpxtrackpoints.each do |p|
162
+ vertices << GoogleMap::Point.new(p.lat, p.lon)
163
+ end
164
+
165
+ # plot polyline
166
+ @map.overlays << GoogleMap::Polyline.new( :map => @map,
167
+ :color=>'#FF0000',
168
+ :weight=>'2',
169
+ :opacity=>'.5',
170
+ :vertices=>vertices
171
+ )
172
+ end
173
+ end
174
+
175
+ maps/show.html.erb
176
+ -------------------------
177
+ <%= @map.to_html %>
178
+ <div style="width: 500px; height: 500px;">
179
+ <%= @map.div %>
180
+ </div>
data/Rakefile ADDED
@@ -0,0 +1,25 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake'
5
+ require 'rake/testtask'
6
+ require 'rake/rdoctask'
7
+
8
+ desc 'Default: run unit tests.'
9
+ task :default => :test
10
+
11
+ desc 'Test the gmap_on_rails plugin.'
12
+ Rake::TestTask.new(:test) do |t|
13
+ t.libs << 'lib'
14
+ t.pattern = 'test/**/*_test.rb'
15
+ t.verbose = true
16
+ end
17
+
18
+ desc 'Generate documentation for the gmap_on_rails plugin.'
19
+ Rake::RDocTask.new(:rdoc) do |rdoc|
20
+ rdoc.rdoc_dir = 'rdoc'
21
+ rdoc.title = 'GMaps on Rails'
22
+ rdoc.options << '--line-numbers' << '--inline-source'
23
+ rdoc.rdoc_files.include('README')
24
+ rdoc.rdoc_files.include('lib/**/*.rb')
25
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "google_map/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "google_map"
7
+ s.version = GoogleMap::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Jeff Dutil"]
10
+ s.email = ["JDutil@BurlingtonWebApps.com"]
11
+ s.homepage = "https://github.com/jdutil/google_map"
12
+ s.summary = %q{Extends geokit and gives convenient helpers for adding google maps to your Rails applicaiton.}
13
+ s.description = %q{Extends geokit and gives convenient helpers for adding google maps to your Rails applicaiton.}
14
+
15
+ s.rubyforge_project = "google_map"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
@@ -0,0 +1,6 @@
1
+ module GoogleMap
2
+
3
+ class BoundingBox
4
+ end
5
+
6
+ end
@@ -0,0 +1,6 @@
1
+ module GoogleMap
2
+
3
+ class Clusterer
4
+ end
5
+
6
+ end
@@ -0,0 +1,22 @@
1
+ module GoogleMap
2
+
3
+ class GeoXml
4
+ #include Reloadable
5
+ include UnbackedDomId
6
+
7
+ attr_accessor :url_of_xml
8
+
9
+ def initialize(options = {})
10
+ options.each_pair { |key, value| send("#{key}=", value) }
11
+ end
12
+
13
+ def to_js
14
+
15
+ js = []
16
+ js << "#{dom_id} = new GGeoXml('#{url_of_xml}');"
17
+
18
+ js.join "\n"
19
+ end
20
+ end
21
+
22
+ end
@@ -0,0 +1,64 @@
1
+ module GoogleMap
2
+
3
+ class Icon
4
+ #include Reloadable
5
+ #include UnbackedDomId
6
+
7
+ attr_accessor :map,
8
+ :dom_id,
9
+ :width,
10
+ :height,
11
+ :shadow_width,
12
+ :shadow_height,
13
+ :image_url,
14
+ :shadow_url,
15
+ :anchor_x,
16
+ :anchor_y,
17
+ :info_anchor_x,
18
+ :info_anchor_y
19
+
20
+ def initialize(options = {})
21
+ self.image_url = 'http://www.google.com/mapfiles/marker.png'
22
+ self.shadow_url = 'http://www.google.com/mapfiles/shadow50.png'
23
+ self.width = 20
24
+ self.height = 34
25
+ self.shadow_width = 37
26
+ self.shadow_height = 34
27
+ self.anchor_x = 6
28
+ self.anchor_y = 20
29
+ self.info_anchor_x = 5
30
+ self.info_anchor_y = 1
31
+ options.each_pair { |key, value| send("#{key}=", value) }
32
+
33
+ if !map or !map.kind_of?(GoogleMap::Map)
34
+ raise "Must set map for GoogleMap::Icon."
35
+ end
36
+ if dom_id.blank?
37
+ self.dom_id = "#{map.dom_id}_icon_#{map.markers.size + 1}"
38
+ end
39
+
40
+ end
41
+
42
+ def to_js
43
+ js = []
44
+ js << "var #{dom_id} = new GIcon();"
45
+ js << "#{dom_id}.image = \"#{image_url}\";"
46
+ js << "#{dom_id}.shadow = \"#{shadow_url}\";"
47
+ js << "#{dom_id}.iconSize = new GSize(#{width}, #{height});"
48
+ js << "#{dom_id}.shadowSize = new GSize(#{shadow_width}, #{shadow_height});"
49
+ js << "#{dom_id}.iconAnchor = new GPoint(#{anchor_x}, #{anchor_y});"
50
+ js << "#{dom_id}.infoWindowAnchor = new GPoint(#{info_anchor_x}, #{info_anchor_y});"
51
+ return js.join("\n")
52
+ end
53
+
54
+ def to_html
55
+ html = []
56
+ html << "<script type=\"text/javascript\">\n/* <![CDATA[ */\n"
57
+ html << to_js
58
+ html << "/* ]]> */</script> "
59
+ return html.join("\n")
60
+ end
61
+
62
+ end
63
+
64
+ end
@@ -0,0 +1,14 @@
1
+ module GoogleMap
2
+
3
+ class LetterIcon < GoogleMap::Icon
4
+ #include Reloadable
5
+
6
+ alias_method :parent_initialize, :initialize
7
+
8
+ def initialize(map, letter)
9
+ parent_initialize(:map=>map, :image_url => "http://www.google.com/mapfiles/marker#{letter}.png")
10
+ end
11
+
12
+ end
13
+
14
+ end
@@ -0,0 +1,430 @@
1
+ module GoogleMap
2
+ class Map
3
+ #include Reloadable
4
+ include UnbackedDomId
5
+ attr_accessor :dom_id,
6
+ :markers,
7
+ :overlays,
8
+ :controls,
9
+ :inject_on_load,
10
+ :zoom,
11
+ :center,
12
+ :double_click_zoom,
13
+ :continuous_zoom,
14
+ :scroll_wheel_zoom,
15
+ :bounds,
16
+ :map_type
17
+
18
+ def initialize(options = {})
19
+ self.dom_id = 'google_map'
20
+ self.markers = []
21
+ self.overlays = []
22
+ self.bounds = []
23
+ self.controls = [ :large, :scale, :type ]
24
+ self.double_click_zoom = true
25
+ self.continuous_zoom = false
26
+ self.scroll_wheel_zoom = false
27
+ options.each_pair { |key, value| send("#{key}=", value) }
28
+ end
29
+
30
+ def to_html
31
+ html = []
32
+
33
+ html << "<script src='http://maps.google.com/maps?file=api&amp;v=2&amp;key=#{GOOGLE_APPLICATION_ID}' type='text/javascript'></script>"
34
+ html << "<script type=\"text/javascript\">\n/* <![CDATA[ */\n"
35
+ html << to_js
36
+ html << "/* ]]> */</script> "
37
+
38
+ return html.join("\n")
39
+ end
40
+
41
+ def to_enable_prefix true_or_false
42
+ true_or_false ? "enable" : "disable"
43
+ end
44
+
45
+ def to_js
46
+ js = []
47
+
48
+ # Initialise the map variable so that it can externally accessed.
49
+ js << "var #{dom_id};"
50
+
51
+ markers.each { |marker| js << "var #{marker.dom_id};" }
52
+
53
+ js << markers_functions_js
54
+ js << center_map_js
55
+
56
+ js << "function initialize_google_map_#{dom_id}() {"
57
+ js << " if(GBrowserIsCompatible()) {"
58
+ js << " #{dom_id} = new GMap2(document.getElementById('#{dom_id}'));"
59
+
60
+ js << " if (self['GoogleMapOnLoad']) {"
61
+ js << " #{dom_id}.load = GEvent.addListener(#{dom_id},'load',GoogleMapOnLoad)"
62
+ js << " }"
63
+
64
+ js << ' ' + map_type_js
65
+ js << ' ' + controls_js
66
+ js << ' ' + center_on_bounds_js
67
+ js << ' ' + markers_icons_js
68
+
69
+ # Put all the markers on the map.
70
+ for marker in markers
71
+ js << ' ' + marker.to_js
72
+ js << ''
73
+ end
74
+
75
+ overlays.each do |overlay|
76
+ js << overlay.to_js
77
+ js << "#{dom_id}.addOverlay(#{overlay.dom_id});"
78
+ end
79
+
80
+ js << "#{dom_id}.#{to_enable_prefix double_click_zoom}DoubleClickZoom();"
81
+ js << "#{dom_id}.#{to_enable_prefix continuous_zoom}ContinuousZoom();"
82
+ js << "#{dom_id}.#{to_enable_prefix scroll_wheel_zoom}ScrollWheelZoom();"
83
+
84
+ js << ' ' + inject_on_load.gsub("\n", " \n") if inject_on_load
85
+ js << " }"
86
+ js << "}"
87
+
88
+ # Load the map on window load preserving anything already on window.onload.
89
+ js << "if (typeof window.onload != 'function') {"
90
+ js << " window.onload = initialize_google_map_#{dom_id};"
91
+ js << "} else {"
92
+ js << " old_before_google_map_#{dom_id} = window.onload;"
93
+ js << " window.onload = function() {"
94
+ js << " old_before_google_map_#{dom_id}();"
95
+ js << " initialize_google_map_#{dom_id}();"
96
+ js << " }"
97
+ js << "}"
98
+
99
+ # Unload the map on window load preserving anything already on window.onunload.
100
+ #js << "if (typeof window.onunload != 'function') {"
101
+ #js << " window.onunload = GUnload();"
102
+ #js << "} else {"
103
+ #js << " old_before_onunload = window.onload;"
104
+ #js << " window.onunload = function() {"
105
+ #js << " old_before_onunload;"
106
+ #js << " GUnload();"
107
+ #js << " }"
108
+ #js << "}"
109
+
110
+ return js.join("\n")
111
+ end
112
+
113
+ def map_type_js
114
+ js = []
115
+ if map_type
116
+ js << "#{dom_id}.setMapType(#{map_type});"
117
+ end
118
+ js.join("\n")
119
+ end
120
+
121
+ def controls_js
122
+ js = []
123
+
124
+ controls.each do |control|
125
+ case control
126
+ when :large, :small, :overview
127
+ c = "G#{control.to_s.capitalize}MapControl"
128
+ when :large_3d
129
+ c = "GLargeMapControl3D"
130
+ when :scale
131
+ c = "GScaleControl"
132
+ when :type
133
+ c = "GMapTypeControl"
134
+ when :menu_type
135
+ c = "GMenuMapTypeControl"
136
+ when :hierachical_type
137
+ c = "GHierarchicalMapTypeControl"
138
+ when :zoom
139
+ c = "GSmallZoomControl"
140
+ when :zoom_3d
141
+ c = "GSmallZoomControl3D"
142
+ when :nav_label
143
+ c = "GNavLabelControl"
144
+ end
145
+ js << "#{dom_id}.addControl(new #{c}());"
146
+ end
147
+
148
+ return js.join("\n")
149
+ end
150
+
151
+ def markers_functions_js
152
+ js = []
153
+ for marker in markers
154
+ js << marker.open_info_window_function
155
+ end
156
+ return js.join("\n")
157
+ end
158
+
159
+ def markers_icons_js
160
+ icons = []
161
+ for marker in markers
162
+ if marker.icon and !icons.include?(marker.icon)
163
+ icons << marker.icon
164
+ end
165
+ end
166
+ js = []
167
+ for icon in icons
168
+ js << icon.to_js
169
+ end
170
+ return js.join("\n")
171
+ end
172
+
173
+ # Creates a JS function that centers the map on the specified center
174
+ # location if given to the initialisers, or on the maps markers if they exist, or
175
+ # at (0,0) if not.
176
+ def center_map_js
177
+ if self.zoom
178
+ zoom_js = zoom
179
+ else
180
+ zoom_js = "#{dom_id}.getBoundsZoomLevel(#{dom_id}_latlng_bounds)"
181
+ end
182
+ set_center_js = []
183
+
184
+ if self.center
185
+ set_center_js << "#{dom_id}.setCenter(new GLatLng(#{center.lat}, #{center.lng}), #{zoom_js});"
186
+ else
187
+ synch_bounds
188
+ set_center_js << "var #{dom_id}_latlng_bounds = new GLatLngBounds();"
189
+
190
+ bounds.each do |point|
191
+ set_center_js << "#{dom_id}_latlng_bounds.extend(new GLatLng(#{point.lat}, #{point.lng}));"
192
+ end
193
+
194
+ set_center_js << "#{dom_id}.setCenter(#{dom_id}_latlng_bounds.getCenter(), #{zoom_js});"
195
+ end
196
+
197
+ "function center_#{dom_id}() {\n #{check_resize_js}\n #{set_center_js.join "\n"}\n}"
198
+ end
199
+
200
+ def synch_bounds
201
+
202
+ overlays.each do |overlay|
203
+ if overlay.is_a? GoogleMap::Polyline
204
+ overlay.vertices.each do |v|
205
+ bounds << v #i do not like this inconsistent interface
206
+ end
207
+ end
208
+ end
209
+
210
+ markers.each do |m|
211
+ bounds << m
212
+ end
213
+
214
+ bounds.uniq!
215
+ end
216
+
217
+ def check_resize_js
218
+ return "#{dom_id}.checkResize();"
219
+ end
220
+
221
+ def center_on_bounds_js
222
+ return "center_#{dom_id}();"
223
+ end
224
+
225
+ def div(width = '100%', height = '100%')
226
+ "<div id='#{dom_id}' style='width: #{width}; height: #{height}'></div>"
227
+ end
228
+
229
+ end
230
+ end
231
+
232
+ # class Map
233
+ # #include Reloadable
234
+ # include UnbackedDomId
235
+ # attr_accessor :dom_id,
236
+ # :markers,
237
+ # :polylines,
238
+ # :controls,
239
+ # :inject_on_load,
240
+ # :zoom
241
+ #
242
+ # def initialize(options = {})
243
+ # self.dom_id = 'google_map'
244
+ # self.markers = []
245
+ # self.polylines = []
246
+ # self.controls = [ :zoom, :overview, :scale, :type ]
247
+ # options.each_pair { |key, value| send("#{key}=", value) }
248
+ # end
249
+ #
250
+ # def to_html
251
+ # html = []
252
+ #
253
+ # html << "<script src='http://maps.google.com/maps?file=api&amp;v=2&amp;key=#{GOOGLE_APPLICATION_ID}' type='text/javascript'></script>"
254
+ # html << "<script type=\"text/javascript\">\n/* <![CDATA[ */\n"
255
+ # html << to_js
256
+ # html << "/* ]]> */</script> "
257
+ #
258
+ # return html.join("\n")
259
+ # end
260
+ #
261
+ # def to_js
262
+ # js = []
263
+ #
264
+ # # Initialise the map variable so that it can externally accessed.
265
+ # js << "var #{dom_id};"
266
+ #
267
+ # markers.each { |marker| js << "var #{marker.dom_id};" }
268
+ #
269
+ # js << markers_functions_js
270
+ #
271
+ # js << center_on_markers_function_js
272
+ #
273
+ # js << "function initialize_google_map_#{dom_id}() {"
274
+ # js << " if(GBrowserIsCompatible()) {"
275
+ # js << " #{dom_id} = new GMap2(document.getElementById('#{dom_id}'));"
276
+ #
277
+ # js << " if (self['GoogleMapOnLoad']) {"
278
+ # # added by Patrick to enable load functions
279
+ # js << "#{dom_id}.load = GEvent.addListener(#{dom_id},'load',GoogleMapOnLoad)"
280
+ # js << "}"
281
+ # js << ' ' + controls_js
282
+ #
283
+ # js << ' ' + center_on_markers_js
284
+ #
285
+ # js << ' ' + markers_icons_js
286
+ #
287
+ # # Put all the markers on the map.
288
+ # for marker in markers
289
+ # js << ' ' + marker.to_js
290
+ # js << ''
291
+ # end
292
+ #
293
+ # for polyline in polylines
294
+ # js << ' ' + polyline.to_js
295
+ # js << ''
296
+ # end
297
+ #
298
+ # js << ' ' + inject_on_load.gsub("\n", " \n") if inject_on_load
299
+ # js << " }"
300
+ # js << "}"
301
+ #
302
+ # # Load the map on window load preserving anything already on window.onload.
303
+ # js << "if (typeof window.onload != 'function') {"
304
+ # js << " window.onload = initialize_google_map_#{dom_id};"
305
+ # js << "} else {"
306
+ # js << " old_before_google_map_#{dom_id} = window.onload;"
307
+ # js << " window.onload = function() {"
308
+ # js << " old_before_google_map_#{dom_id}();"
309
+ # js << " initialize_google_map_#{dom_id}();"
310
+ # js << " }"
311
+ # js << "}"
312
+ #
313
+ # # Unload the map on window load preserving anything already on window.onunload.
314
+ # #js << "if (typeof window.onunload != 'function') {"
315
+ # #js << " window.onunload = GUnload();"
316
+ # #js << "} else {"
317
+ # #js << " old_before_onunload = window.onload;"
318
+ # #js << " window.onunload = function() {"
319
+ # #js << " old_before_onunload;"
320
+ # #js << " GUnload();"
321
+ # #js << " }"
322
+ # #js << "}"
323
+ #
324
+ # return js.join("\n")
325
+ # end
326
+ #
327
+ # def controls_js
328
+ # js = []
329
+ #
330
+ # controls.each do |control|
331
+ # case control
332
+ # when :large, :small, :overview
333
+ # c = "G#{control.to_s.capitalize}MapControl"
334
+ # when :scale
335
+ # c = "GScaleControl"
336
+ # when :type
337
+ # c = "GMapTypeControl"
338
+ # when :zoom
339
+ # c = "GSmallZoomControl"
340
+ # end
341
+ # js << "#{dom_id}.addControl(new #{c}());"
342
+ # end
343
+ #
344
+ # return js.join("\n")
345
+ # end
346
+ #
347
+ # def markers_functions_js
348
+ # js = []
349
+ #
350
+ # for marker in markers
351
+ # js << marker.open_info_window_function
352
+ # end
353
+ #
354
+ # return js.join("\n")
355
+ # end
356
+ #
357
+ # def markers_icons_js
358
+ # icons = []
359
+ #
360
+ # for marker in markers
361
+ # if marker.icon and !icons.include?(marker.icon)
362
+ # icons << marker.icon
363
+ # end
364
+ # end
365
+ #
366
+ # js = []
367
+ #
368
+ # for icon in icons
369
+ # js << icon.to_js
370
+ # end
371
+ #
372
+ # return js.join("\n")
373
+ # end
374
+ #
375
+ # # Creates a JS function that centers the map on its markers.
376
+ # def center_on_markers_function_js
377
+ # if markers.size == 0 and polylines.size == 0
378
+ # set_center_js = "#{dom_id}.setCenter(new GLatLng(0, 0), 0);"
379
+ # else
380
+ #
381
+ # for marker in markers
382
+ # min_lat = marker.lat if !min_lat or marker.lat < min_lat
383
+ # max_lat = marker.lat if !max_lat or marker.lat > max_lat
384
+ # min_lng = marker.lng if !min_lng or marker.lng < min_lng
385
+ # max_lng = marker.lng if !max_lng or marker.lng > max_lng
386
+ # end
387
+ #
388
+ # for polyline in polylines
389
+ # polyline.points.each do |point|
390
+ # min_lat = point.lat if !min_lat or point.lat < min_lat
391
+ # max_lat = point.lat if !max_lat or point.lat > max_lat
392
+ # min_lng = point.lng if !min_lng or point.lng < min_lng
393
+ # max_lng = point.lng if !max_lng or point.lng > max_lng
394
+ # end
395
+ # end
396
+ #
397
+ # # if no markers or polyline points zero values
398
+ # min_lat ? min_lat : 0
399
+ # max_lat ? max_lat : 0
400
+ # min_lng ? min_lng : 0
401
+ # max_lng ? max_lng : 0
402
+ #
403
+ # if self.zoom
404
+ # zoom_js = zoom
405
+ # else
406
+ # bounds_js = "new GLatLngBounds(new GLatLng(#{min_lat}, #{min_lng}), new GLatLng(#{max_lat}, #{max_lng}))"
407
+ # zoom_js = "#{dom_id}.getBoundsZoomLevel(#{bounds_js})"
408
+ # end
409
+ #
410
+ # center_js = "new GLatLng(#{(min_lat + max_lat) / 2}, #{(min_lng + max_lng) / 2})"
411
+ # set_center_js = "#{dom_id}.setCenter(#{center_js}, #{zoom_js});"
412
+ # end
413
+ #
414
+ # return "function center_#{dom_id}() {\n #{check_resize_js}\n #{set_center_js}\n}"
415
+ # end
416
+ #
417
+ # def check_resize_js
418
+ # return "#{dom_id}.checkResize();"
419
+ # end
420
+ #
421
+ # def center_on_markers_js
422
+ # return "center_#{dom_id}();"
423
+ # end
424
+ #
425
+ # def div(width = '100%', height = '100%')
426
+ # "<div id='#{dom_id}' style='width: #{width}; height: #{height}'></div>"
427
+ # end
428
+ #
429
+ # end
430
+
@@ -0,0 +1,77 @@
1
+ module GoogleMap
2
+
3
+ class Marker
4
+
5
+ include ActionView::Helpers::JavaScriptHelper
6
+
7
+ attr_accessor :dom_id,
8
+ :lat,
9
+ :lng,
10
+ :html,
11
+ :marker_icon_path,
12
+ :marker_hover_text,
13
+ :map,
14
+ :icon,
15
+ :open_infoWindow,
16
+ :draggable,
17
+ :dragstart,
18
+ :dragend
19
+
20
+ def initialize(options = {})
21
+ options.each_pair { |key, value| send("#{key}=", value) }
22
+
23
+ if lat.blank? or lng.blank? or !map or !map.kind_of?(GoogleMap::Map)
24
+ raise "Must set lat, lng, and map for GoogleMapMarker."
25
+ end
26
+
27
+ if dom_id.blank?
28
+ # This needs self to set the attr_accessor, why?
29
+ self.dom_id = "#{map.dom_id}_marker_#{map.markers.size + 1}"
30
+ end
31
+
32
+ end
33
+
34
+ def open_info_window_function
35
+ js = []
36
+
37
+ js << "function #{dom_id}_infowindow_function() {"
38
+ js << " #{dom_id}.openInfoWindowHtml(\"#{escape_javascript(html)}\")"
39
+ js << "}"
40
+
41
+ return js.join("\n")
42
+ end
43
+
44
+ def open_info_window
45
+ "#{dom_id}_infowindow_function();"
46
+ end
47
+
48
+ def to_js
49
+ js = []
50
+
51
+ h = ", title: '#{escape_javascript(marker_hover_text)}'" if marker_hover_text
52
+
53
+ # If a icon is specified, use it in marker creation.
54
+ i = ", { icon: #{icon.dom_id} #{h} }" if icon
55
+ i = ", { icon: new GIcon( G_DEFAULT_ICON, '#{marker_icon_path}') #{h} }" if marker_icon_path
56
+
57
+ options = ', { draggable: true }' if self.draggable
58
+ js << "#{dom_id} = new GMarker( new GLatLng( #{lat}, #{lng} ) #{i} #{options} );"
59
+ js << "GEvent.bind(#{dom_id}, \"dragstart\", #{dom_id}, #{self.dragstart});" if dragstart
60
+ js << "GEvent.bind(#{dom_id}, \"dragend\", #{dom_id}, #{self.dragend});" if dragend
61
+
62
+ if self.html
63
+ js << "GEvent.addListener(#{dom_id}, 'click', function() {#{dom_id}_infowindow_function()});"
64
+ end
65
+
66
+ js << "#{map.dom_id}.addOverlay(#{dom_id});"
67
+
68
+ if open_infoWindow
69
+ js << "GEvent.trigger(#{dom_id}, 'click')"
70
+ end
71
+
72
+ return js.join("\n")
73
+ end
74
+
75
+ end
76
+
77
+ end
@@ -0,0 +1,6 @@
1
+ module GoogleMap
2
+
3
+ class MarkerGroup
4
+ end
5
+
6
+ end
@@ -0,0 +1,22 @@
1
+ module GoogleMap
2
+
3
+ class Point
4
+ attr_accessor :lat,
5
+ :lng
6
+
7
+ def initialize(lat, lng)
8
+ self.lat = lat
9
+ self.lng = lng
10
+ if lat.blank? or lng.blank?
11
+ raise "Must set lat, lng for GoogleMap::Point."
12
+ end
13
+
14
+ end
15
+
16
+ def to_js
17
+ return "new GLatLng(#{self.lat}, #{self.lng})"
18
+ end
19
+
20
+ end
21
+
22
+ end
@@ -0,0 +1,43 @@
1
+ module GoogleMap
2
+
3
+ class Polyline
4
+ #include Reloadable
5
+ include UnbackedDomId
6
+
7
+ attr_accessor :dom_id,
8
+ :map,
9
+ :vertices,
10
+ :color,
11
+ :weight,
12
+ :opacity
13
+
14
+ def initialize(options = {})
15
+ self.vertices = []
16
+ self.color = "#000"
17
+ self.weight = 1
18
+ self.opacity = 1
19
+ options.each_pair { |key, value| send("#{key}=", value) }
20
+ if !map or !map.kind_of?(GoogleMap::Map)
21
+ raise "Must set map for GoogleMap::Polyline."
22
+ end
23
+ if dom_id.blank?
24
+ # This needs self to set the attr_accessor, why?
25
+ self.dom_id = "#{map.dom_id}_marker_#{map.markers.size + 1}"
26
+ end
27
+ end
28
+
29
+ def to_js
30
+
31
+ js = []
32
+ js << "#{dom_id}_vertices = new Array();"
33
+ vertices.each_with_index do |point, index|
34
+ js << "#{dom_id}_vertices[#{index}] = new GLatLng(#{point.lat}, #{point.lng});"
35
+ end
36
+
37
+ js << "#{dom_id} = new GPolyline(#{dom_id}_vertices, '#{color}', #{weight}, #{opacity});"
38
+
39
+ js.join "\n"
40
+ end
41
+ end
42
+
43
+ end
@@ -0,0 +1,23 @@
1
+ module GoogleMap
2
+
3
+ class SmallIcon < GoogleMap::Icon
4
+ #include Reloadable
5
+
6
+ alias_method :parent_initialize, :initialize
7
+
8
+ def initialize( map, color = 'red')
9
+ parent_initialize(:width => 12,
10
+ :height => 20,
11
+ :shadow_width => 22,
12
+ :shadow_height => 20,
13
+ :image_url => "http://labs.google.com/ridefinder/images/mm_20_#{color}.png",
14
+ :shadow_url => "http://labs.google.com/ridefinder/images/mm_20_shadow.png",
15
+ :anchor_x => 6,
16
+ :anchor_y => 20,
17
+ :info_anchor_x => 5,
18
+ :info_anchor_y => 1,
19
+ :map => map)
20
+ end
21
+ end
22
+
23
+ end
@@ -0,0 +1,3 @@
1
+ module GoogleMap
2
+ VERSION = "0.0.1"
3
+ end
data/lib/google_map.rb ADDED
@@ -0,0 +1,13 @@
1
+ module GoogleMap
2
+ # require 'google_map/bounding_box'
3
+ # require 'google_map/clusterer'
4
+ require 'google_map/geo_xml'
5
+ require 'google_map/icon'
6
+ require 'google_map/letter_icon'
7
+ require 'google_map/map'
8
+ require 'google_map/marker'
9
+ require 'google_map/marker_group'
10
+ require 'google_map/point'
11
+ require 'google_map/polyline'
12
+ require 'google_map/small_icon'
13
+ end
@@ -0,0 +1,22 @@
1
+ # UnbackedDomId
2
+ module UnbackedDomId
3
+ ##
4
+ # Implementation of http://codefluency.com/articles/2006/05/30/rails-views-dom-id-scheme
5
+ #
6
+ # my_special_race_car.dom_id
7
+ # => "race_car_1"
8
+ #
9
+ # another_race_car.dom_id
10
+ # => "race_car_2"
11
+ #
12
+ def dom_id(prefix=nil)
13
+ id = self.object_id.to_s.gsub('-', '_')
14
+ class_name = self.class.name.gsub(/::/, '/').
15
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
16
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
17
+ tr("-", "_").
18
+ downcase
19
+ prefix = prefix.nil? ? class_name : "#{prefix}_#{class_name}"
20
+ "#{prefix}_#{id}"
21
+ end
22
+ end
@@ -0,0 +1,65 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ GOOGLE_APPLICATION_ID = "ABQIAAAA3HdfrnxFAPWyY-aiJUxmqRTJQa0g3IQ9GZqIMmInSLzwtGDKaBQ0KYLwBEKSM7F9gCevcsIf6WPuIQ"
4
+
5
+ class GoogleMapTest < Test::Unit::TestCase
6
+ def setup
7
+ @map = GoogleMap::Map.new
8
+ end
9
+
10
+ def test_new_map_has_empty_markers
11
+ assert @map.markers.empty?
12
+ end
13
+
14
+ def test_add_markers
15
+ (1..5).each do |i|
16
+ @map.markers << marker_factory
17
+ assert_equal @map.markers.length, i
18
+ # puts @map.to_html
19
+ assert @map.to_html.include? " google_map_marker_#{i} = new GMarker( new GLatLng( 40, -100 ) );"
20
+ end
21
+ end
22
+
23
+ def test_center_on_markers_function_for_empty_map
24
+ assert @map.center_map_js.include? "google_map.setCenter(google_map_latlng_bounds.getCenter(), google_map.getBoundsZoomLevel(google_map_latlng_bounds));"
25
+ end
26
+
27
+ def test_center_on_markers_function_for_one_marker
28
+ @map.markers << marker_factory
29
+ assert @map.center_map_js.include? "google_map_latlng_bounds.extend(new GLatLng(40, -100));"
30
+ end
31
+
32
+ def test_center_on_markers_function_for_two_markers
33
+ @map.markers << marker_factory
34
+ @map.markers << marker_factory({:lng => 100})
35
+ assert @map.center_map_js.include? "google_map_latlng_bounds.extend(new GLatLng(40, 100));"
36
+ end
37
+
38
+ def test_set_center_with_options
39
+ @map = GoogleMap::Map.new({:center => GoogleMap::Point.new(10, 10)})
40
+ @map.markers << marker_factory
41
+ assert @map.center_map_js.include? "new GLatLng(10, 10)"
42
+ end
43
+
44
+ def test_add_polylines
45
+ (1..5).each do |i|
46
+ @map.overlays << polyline_factory
47
+ assert_equal @map.overlays.length, i
48
+ assert @map.to_html.include? "#{@map.overlays[i - 1].dom_id} = new GPolyline(#{@map.overlays[i - 1].dom_id}_vertices, '#00FF00', 10, 2);"
49
+ end
50
+ end
51
+
52
+ def test_add_geoxml
53
+ (1..5).each do |i|
54
+ @map.overlays << geoxml_factory
55
+ assert_equal @map.overlays.length, i
56
+ assert @map.to_html.include? "#{@map.overlays[i - 1].dom_id} = new GGeoXml('http://code.google.com/apis/kml/documentation/KML_Samples.kml')";
57
+ end
58
+ end
59
+
60
+ def test_map_type
61
+ assert !@map.to_html.include?("setMapType(GoogleMap)")
62
+ @map.map_type = "foo"
63
+ assert @map.to_html.include? "setMapType(foo)"
64
+ end
65
+ end
@@ -0,0 +1,52 @@
1
+ ENV['RAILS_ENV'] = 'test'
2
+
3
+ require 'test/unit'
4
+ require 'rubygems'
5
+ require 'action_pack'
6
+ require 'action_view'
7
+ require 'lib/unbacked_dom_id'
8
+ require 'lib/google_map'
9
+ require 'lib/google_map/bounding_box'
10
+ require 'lib/google_map/clusterer'
11
+ require 'lib/google_map/geo_xml'
12
+ require 'lib/google_map/icon'
13
+ require 'lib/google_map/letter_icon'
14
+ require 'lib/google_map/map'
15
+ require 'lib/google_map/marker'
16
+ require 'lib/google_map/marker_group'
17
+ require 'lib/google_map/point'
18
+ require 'lib/google_map/polyline'
19
+ require 'lib/google_map/small_icon'
20
+
21
+ def marker_factory(options = {})
22
+ params = {
23
+ :map => @map,
24
+ :lat => 40,
25
+ :lng => -100,
26
+ :html => 'Test Marker'
27
+ }.merge(options)
28
+ GoogleMap::Marker.new(params)
29
+ end
30
+
31
+ def polyline_factory(options = {})
32
+ params = {
33
+ :map => @map,
34
+ :color => "#00FF00",
35
+ :weight => 10,
36
+ :opacity => 2,
37
+ :vertices => [
38
+ GoogleMap::Point.new(40, -100),
39
+ GoogleMap::Point.new(40, 100)]
40
+ }.merge(options)
41
+ GoogleMap::Polyline.new(params)
42
+ end
43
+
44
+ def geoxml_factory(options = {})
45
+ params = {
46
+ :url_of_xml => "http://code.google.com/apis/kml/documentation/KML_Samples.kml"
47
+ }.merge(options)
48
+ GoogleMap::GeoXml.new(params)
49
+ end
50
+
51
+
52
+
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: google_map
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Jeff Dutil
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-22 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Extends geokit and gives convenient helpers for adding google maps to your Rails applicaiton.
23
+ email:
24
+ - JDutil@BurlingtonWebApps.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - MIT_LICENSE.txt
35
+ - README
36
+ - Rakefile
37
+ - google_map.gemspec
38
+ - lib/google_map.rb
39
+ - lib/google_map/bounding_box.rb
40
+ - lib/google_map/clusterer.rb
41
+ - lib/google_map/geo_xml.rb
42
+ - lib/google_map/icon.rb
43
+ - lib/google_map/letter_icon.rb
44
+ - lib/google_map/map.rb
45
+ - lib/google_map/marker.rb
46
+ - lib/google_map/marker_group.rb
47
+ - lib/google_map/point.rb
48
+ - lib/google_map/polyline.rb
49
+ - lib/google_map/small_icon.rb
50
+ - lib/google_map/version.rb
51
+ - lib/unbacked_dom_id.rb
52
+ - test/google_map_test.rb
53
+ - test/test_helper.rb
54
+ has_rdoc: true
55
+ homepage: https://github.com/jdutil/google_map
56
+ licenses: []
57
+
58
+ post_install_message:
59
+ rdoc_options: []
60
+
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ hash: 3
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ requirements: []
82
+
83
+ rubyforge_project: google_map
84
+ rubygems_version: 1.5.0
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Extends geokit and gives convenient helpers for adding google maps to your Rails applicaiton.
88
+ test_files:
89
+ - test/google_map_test.rb
90
+ - test/test_helper.rb