Class: Google4R::Maps::GMap2

Inherits:
Object
  • Object
show all
Defined in:
lib/google4r/maps.rb

Overview

GMap2 instances can be converted to the HTML/JS that is necessary to render a GMap2 widget.

The API documentation provided by Google will be insightful: www.google.com/apis/maps/documentation/reference.html#GMap2

Example

map = GMap2.new("map", "map")
map.to_html # => %q{<div id="map"></div><script type="text/javascript">var map = new GMap2...}

– TODO: Use GMarkerManager instead of adding Markers directly? ++

Constant Summary collapse

DEFAULT_ZOOM =

Default for zoom if zoom is :auto and no markers have been added.

13
DEFAULT_CENTER =

Default for center if center is :auto and no markers have been added.

[ 0, 0 ].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, div_id) ⇒ GMap2

Create a new MapWidget instance.

Parameters

name

The name of the Javascript variable to create to hold the GMap2 instance.

div_id

The id of the <div /> tag to create.

options

A hash with options.

Valid options entries are:

:size

Array ([ width, height ]) with two integers. By default, the map will use the size of its container.

:draggable_cursor

A string with the CSS name of the cursor to use when dragging the map.

:dragging_cursor

A string with the CSS name of the cursor to use when the map is draggable.

++

Example

GMap2.new(“map”, “map”)



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/google4r/maps.rb', line 183

def initialize(name, div_id)
  @icons = Array.new
  @markers = Array.new
  @options = { :register_gunload => true }
  
  # defaults
  @controls = [ :large_map, :scale, :map_type ]
  @map_type = :normal
  @dragging_enabled = true
  @info_window_enabled = true
  @double_click_zoom_enabled = false
  @continuous_zoom_enabled = false
  @center = :auto
  @zoom = :auto
  @onload_js = nil
  
  # initialization
  @name = name
  @div_id = div_id
end

Instance Attribute Details

#centerObject

Array with [ latitude, longitude ] of the center of the map. Defaults to :auto. If set to :auto then the map will be centered over all added markers. If set to :auto and no markers have been added then the value [ 0, 0 ] will be used.



153
154
155
# File 'lib/google4r/maps.rb', line 153

def center
  @center
end

#continuous_zoom_enabledObject

true iff smooth zooming is enabled - defaults to false



138
139
140
# File 'lib/google4r/maps.rb', line 138

def continuous_zoom_enabled
  @continuous_zoom_enabled
end

#controlsObject

An array with control to display on the map. Valid entries are :small_map, :large_map, :small_zoom, :scale, :map_type. They are directly mapped to the controls documented on www.google.com/apis/maps/documentation/reference.html#GControlImpl. Defaults to [ :large_map, :scale, :map_type ].



121
122
123
# File 'lib/google4r/maps.rb', line 121

def controls
  @controls
end

#div_idObject

The id of the <div> id to create.



103
104
105
# File 'lib/google4r/maps.rb', line 103

def div_id
  @div_id
end

#double_click_zoom_enabledObject

true iff zooming by double clicking is enabled - defaults to false



135
136
137
# File 'lib/google4r/maps.rb', line 135

def double_click_zoom_enabled
  @double_click_zoom_enabled
end

#draggable_cursorObject

String with the CSS name of the cursor to use when dragging the map. Defaults to nil.



111
112
113
# File 'lib/google4r/maps.rb', line 111

def draggable_cursor
  @draggable_cursor
end

#dragging_cursorObject

String with the CSS name of the cursor to use when the map is draggable. Defaults to nil.



115
116
117
# File 'lib/google4r/maps.rb', line 115

def dragging_cursor
  @dragging_cursor
end

#dragging_enabledObject

true iff dragging is enabled - defaults to true



129
130
131
# File 'lib/google4r/maps.rb', line 129

def dragging_enabled
  @dragging_enabled
end

#iconsObject (readonly)

An array of GIcon objects. Use #create_icon to create new GIcon object.



94
95
96
# File 'lib/google4r/maps.rb', line 94

def icons
  @icons
end

#info_window_enabledObject

true iff info windows are show - defaults to true



132
133
134
# File 'lib/google4r/maps.rb', line 132

def info_window_enabled
  @info_window_enabled
end

#map_typeObject

The map types to allow by default. Can be one of :normal, :satellite and :hybrid. These symbols are mapped to G_NORMAL_MAP, G_SATELLITE_MAP and G_HYBRID_MAP in JS. Defaults to :normal.



126
127
128
# File 'lib/google4r/maps.rb', line 126

def map_type
  @map_type
end

#markersObject (readonly)

An array of GMarker objects. Use #create_marker to create new GMarke object.



97
98
99
# File 'lib/google4r/maps.rb', line 97

def markers
  @markers
end

#nameObject

The name of the Javascript variable to create to hold the GMap2 instance.



100
101
102
# File 'lib/google4r/maps.rb', line 100

def name
  @name
end

#onload_jsObject

String with arbitrary Javascript that you want executed after the map has been initialized and all markers have been placed on the map. Defaults to nil.



142
143
144
# File 'lib/google4r/maps.rb', line 142

def onload_js
  @onload_js
end

#optionsObject (readonly)

Hash with options that do not directly map into GMap2 properties and that only influence the Javascript that is generated.

At the moment, valid keys are:

:register_gunload

Boolean. true iff to add a callback to GUnload() to the “onunload” event of <body>. Defaults to true.



162
163
164
# File 'lib/google4r/maps.rb', line 162

def options
  @options
end

#sizeObject

Array with the size of the map: [ width, height ]. The map will use its container’s size if unset. Defaults to nil.



107
108
109
# File 'lib/google4r/maps.rb', line 107

def size
  @size
end

#zoomObject

Zoom level of the map. Defaults to :auto. If set to :auto then the map is zoomed so that all markers are visible if the map is centered over them. Ranges from 17 (closest to earth) to 0 (world view). If no markers have been added and zoom is :auto then zoom is set to 13.



148
149
150
# File 'lib/google4r/maps.rb', line 148

def zoom
  @zoom
end

Instance Method Details

#create_icon(*options) ⇒ Object

Creates a new GIcon instance that is then available for the GMarker instances on this GMap2.

The parameters passed to this method are the same as to the constructor of the GIcon class.



340
341
342
343
344
# File 'lib/google4r/maps.rb', line 340

def create_icon(*options)
  result = GIcon.new(*options)
  @icons << result
  return result
end

#create_marker(*options) ⇒ Object

Creates a new GMarker instance on this GMap2 instance. Use this factory method to create your GMarker instances instead of the GMarker constructor directly.

The parameters passed to this method are the same as the ones passed to the constructor of the GMarker instance.



351
352
353
354
355
# File 'lib/google4r/maps.rb', line 351

def create_marker(*options)
  result = GMarker.new(*options)
  @markers << result
  return result
end

#to_htmlObject

Creates the <div> tag and the <script> tag and puts self.to_js into the <script> tag.



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/google4r/maps.rb', line 206

def to_html
  result = Array.new
  result << %Q{<div id="#{@div_id}"></div>}
  result << %Q{<script type="text/javascript" charset="utf-8">}
  result << %Q{//<![CDATA[}
  result << %Q{/* Create a variable to hold the GMap2 instance and the icons in. */}
  result << %Q{var #{@name};}
  result << %Q{var #{@name}_icons;}
  result << ""
  # Yes, there are some really nice things that MSIE forces you to do!
  result << %Q|function #{@name}_loader() {|
  result << self.to_js
  result << %Q|}|
  result << ""
  result << %Q|if (window.addEventListener) { /* not MSIE */|
  result << %Q|  window.addEventListener('load', function() { #{@name}_loader(); }, false);|
  result << %Q|} else { /* MSIE */|
  result << %Q|  window.attachEvent('onload', function() { #{@name}_loader(); }, false);|
  result << %Q|}|
  result << ""

  # Add optional Javascript - like the GUnload() call.
  result << %Q|/* Optional Javascript */|
  if options[:register_gunload] then
    result << %Q|if (window.addEventListener) { /* not MSIE */|
    result << %Q|  window.addEventListener('unload', function() { GUnload(); }, false);|
    result << %Q|} else { /* MSIE */|
    result << %Q|  window.attachEvent('onunload', function() { GUnload(); }, false);|
    result << %Q|}|
  end
  
  result << %Q{// ]]>}
  result << %Q{</script>}
  
  return result.join("\n")
end

#to_jsObject

Creates the necessary HTML for the GMap.



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/google4r/maps.rb', line 244

def to_js
  lines = Array.new
  
  # Create GMap2 instance.
  options = []
  options << %Q{draggableCursor: "#{@draggable_cursor}"} unless @draggable_cursor.nil?
  options << %Q{draggingCursor: "#{@dragging_cursor}"} unless @dragging_cursor.nil?
  options << %Q{size: new GSize(#{@size[0]}, #{@size[1]})} unless @size.nil?
  
  # Calculate the center value if the map is to be centered over all markers.
  center = (@center == :auto) ? self.calculate_auto_center : @center
  
  # Let the JS GMap2 object resolve automatic zoom levels if necessary.
  zoom = 
    if @zoom == :auto then
      if @markers.length == 0 then
        DEFAULT_ZOOM
      else
        bounds = self.calculate_auto_bounds
        "#{@name}.getBoundsZoomLevel(new GLatLngBounds(new GLatLng(#{bounds[0]}, #{bounds[1]}), new GLatLng(#{bounds[2]}, #{bounds[3]})))"
      end
    else
      @zoom
    end

  lines << %Q{/* Create GMap2 instance. */}
  lines << %Q{#{@name} = new GMap2(document.getElementById("#{@div_id}"), { #{options.join(', ')} });}
  lines << %Q{#{@name}.setCenter(new GLatLng(#{center[0]}, #{center[1]}), #{zoom});}
  lines << ""
  
  # Set map options.
  lines << %Q{/* Set map options. */}
  lines << %Q{#{@name}.setMapType(G_#{@map_type.to_s.upcase}_MAP);}
  
  if @dragging_enabled then
    lines << %Q{#{@name}.enableDragging();}
  else
    lines << %Q{#{@name}.disableDragging();}
  end
  if @info_window_enabled then
    lines << %Q{#{@name}.enableInfoWindow();}
  else
    lines << %Q{#{@name}.disableInfoWindow();}
  end
  if @double_click_zoom_enabled then
    lines << %Q{#{@name}.enableDoubleClickZoom();}
  else
    lines << %Q{#{@name}.disableDoubleClickZoom();}
  end
  if @continuous_zoom_enabled then
    lines << %Q{#{@name}.enableContinuousZoom();}
  else
    lines << %Q{#{@name}.disableContinuousZoom();}
  end
  lines << ""
  
  # Add controls.
  lines << %Q{/* Add controls to the map. */}
  self.controls.each { |control| lines << %Q{#{@name}.addControl(new #{control_constructor(control)});} }
  lines << ""
  
  # Add icons.
  lines << %Q{/* Create global variable holding all icons. */}
  lines << %Q{#{@name}_icons = new Array();}
  self.icons.each { |icon| lines << "#{@name}_icons.push(#{icon.to_js}());"}
  lines << ""
  
  # Add markers.
  lines << %Q{/* Add markers to the map. */}
  self.markers.each do |marker|
    icon_index = icons.index(marker.icon)
    icon_str =
      if icon_index.nil? then
        ""
      else
       "#{@name}_icons[#{icon_index}]"
      end 
    lines << %Q{#{@name}.addOverlay(#{marker.to_js}(#{icon_str}));}
  end
  lines << ""
  
  # Add user supplied Javascript.
  lines << %Q{/* User supplied Javascript */}
  if not @onload_js.nil? then
    lines << @onload_js.to_s
  end
  lines << ""
  
  return lines.join("\n")
end