Class: GoogleMap::Map

Inherits:
Object
  • Object
show all
Includes:
UnbackedDomId
Defined in:
lib/google_map/map.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Map

Returns a new instance of Map.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/google_map/map.rb', line 18

def initialize(options = {})
  self.dom_id = 'google_map'
  self.markers = []
  self.overlays = []
  self.bounds = []
  self.controls = [ :large, :scale, :type ]
  self.double_click_zoom = true
  self.continuous_zoom = false
  self.scroll_wheel_zoom = false
  options.each_pair { |key, value| send("#{key}=", value) }
end

Instance Attribute Details

#boundsObject

Returns the value of attribute bounds.



5
6
7
# File 'lib/google_map/map.rb', line 5

def bounds
  @bounds
end

#centerObject

Returns the value of attribute center.



5
6
7
# File 'lib/google_map/map.rb', line 5

def center
  @center
end

#continuous_zoomObject

Returns the value of attribute continuous_zoom.



5
6
7
# File 'lib/google_map/map.rb', line 5

def continuous_zoom
  @continuous_zoom
end

#controlsObject

Returns the value of attribute controls.



5
6
7
# File 'lib/google_map/map.rb', line 5

def controls
  @controls
end

#dom_idObject

Returns the value of attribute dom_id.



5
6
7
# File 'lib/google_map/map.rb', line 5

def dom_id
  @dom_id
end

#double_click_zoomObject

Returns the value of attribute double_click_zoom.



5
6
7
# File 'lib/google_map/map.rb', line 5

def double_click_zoom
  @double_click_zoom
end

#inject_on_loadObject

Returns the value of attribute inject_on_load.



5
6
7
# File 'lib/google_map/map.rb', line 5

def inject_on_load
  @inject_on_load
end

#map_typeObject

Returns the value of attribute map_type.



5
6
7
# File 'lib/google_map/map.rb', line 5

def map_type
  @map_type
end

#markersObject

Returns the value of attribute markers.



5
6
7
# File 'lib/google_map/map.rb', line 5

def markers
  @markers
end

#overlaysObject

Returns the value of attribute overlays.



5
6
7
# File 'lib/google_map/map.rb', line 5

def overlays
  @overlays
end

#scroll_wheel_zoomObject

Returns the value of attribute scroll_wheel_zoom.



5
6
7
# File 'lib/google_map/map.rb', line 5

def scroll_wheel_zoom
  @scroll_wheel_zoom
end

#zoomObject

Returns the value of attribute zoom.



5
6
7
# File 'lib/google_map/map.rb', line 5

def zoom
  @zoom
end

Instance Method Details

#center_map_jsObject

Creates a JS function that centers the map on the specified center location if given to the initialisers, or on the maps markers if they exist, or at (0,0) if not.



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/google_map/map.rb', line 176

def center_map_js
  if self.zoom
    zoom_js = zoom
  else
    zoom_js = "#{dom_id}.getBoundsZoomLevel(#{dom_id}_latlng_bounds)"
  end
  set_center_js = []
  
  if self.center
    set_center_js << "#{dom_id}.setCenter(new GLatLng(#{center.lat}, #{center.lng}), #{zoom_js});"
  else
    synch_bounds
    set_center_js << "var #{dom_id}_latlng_bounds = new GLatLngBounds();"
    
    bounds.each do |point|
      set_center_js << "#{dom_id}_latlng_bounds.extend(new GLatLng(#{point.lat}, #{point.lng}));"
    end  
    
    set_center_js << "#{dom_id}.setCenter(#{dom_id}_latlng_bounds.getCenter(), #{zoom_js});"
  end
  
  "function center_#{dom_id}() {\n  #{check_resize_js}\n  #{set_center_js.join "\n"}\n}"
end

#center_on_bounds_jsObject



221
222
223
# File 'lib/google_map/map.rb', line 221

def center_on_bounds_js
  return "center_#{dom_id}();"
end

#check_resize_jsObject



217
218
219
# File 'lib/google_map/map.rb', line 217

def check_resize_js
  return "#{dom_id}.checkResize();"
end

#controls_jsObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/google_map/map.rb', line 121

def controls_js
  js = []

  controls.each do |control|
    case control
    when :large, :small, :overview
      c = "G#{control.to_s.capitalize}MapControl"
    when :large_3d
      c = "GLargeMapControl3D"
    when :scale
      c = "GScaleControl"
    when :type
      c = "GMapTypeControl"
    when :menu_type
      c = "GMenuMapTypeControl"
    when :hierachical_type
      c = "GHierarchicalMapTypeControl"
    when :zoom
      c = "GSmallZoomControl"
    when :zoom_3d
      c = "GSmallZoomControl3D"
    when :nav_label
      c = "GNavLabelControl"
    end
    js << "#{dom_id}.addControl(new #{c}());"
  end

  return js.join("\n")
end

#div(width = '100%', height = '100%') ⇒ Object



225
226
227
# File 'lib/google_map/map.rb', line 225

def div(width = '100%', height = '100%')
  "<div id='#{dom_id}' style='width: #{width}; height: #{height}'></div>"
end

#map_type_jsObject



113
114
115
116
117
118
119
# File 'lib/google_map/map.rb', line 113

def map_type_js
  js = []
  if map_type
    js << "#{dom_id}.setMapType(#{map_type});"
  end    
  js.join("\n")
end

#markers_functions_jsObject



151
152
153
154
155
156
157
# File 'lib/google_map/map.rb', line 151

def markers_functions_js
  js = []
  for marker in markers
    js << marker.open_info_window_function
  end
  return js.join("\n")
end

#markers_icons_jsObject



159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/google_map/map.rb', line 159

def markers_icons_js
  icons = []
  for marker in markers
    if marker.icon and !icons.include?(marker.icon)
      icons << marker.icon 
    end
  end
  js = []
  for icon in icons
    js << icon.to_js
  end
  return js.join("\n")
end

#synch_boundsObject



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/google_map/map.rb', line 200

def synch_bounds
  
  overlays.each do |overlay|
    if overlay.is_a? GoogleMap::Polyline
      overlay.vertices.each do |v|
        bounds << v #i do not like this inconsistent interface
      end 
    end
  end
  
  markers.each do |m|
    bounds << m
  end    
  
  bounds.uniq!
end

#to_enable_prefix(true_or_false) ⇒ Object



41
42
43
# File 'lib/google_map/map.rb', line 41

def to_enable_prefix true_or_false
  true_or_false ? "enable" : "disable"
end

#to_htmlObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/google_map/map.rb', line 30

def to_html
  html = []

  html << "<script src='http://maps.google.com/maps?file=api&amp;v=2&amp;key=#{GOOGLE_APPLICATION_ID}' type='text/javascript'></script>"    
  html << "<script type=\"text/javascript\">\n/* <![CDATA[ */\n"  
  html << to_js
  html << "/* ]]> */</script> "

  return html.join("\n")
end

#to_jsObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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/google_map/map.rb', line 45

def to_js
  js = []

  # Initialise the map variable so that it can externally accessed.
  js << "var #{dom_id};"
  
  markers.each { |marker| js << "var #{marker.dom_id};" }

  js << markers_functions_js
  js << center_map_js

  js << "function initialize_google_map_#{dom_id}() {"
  js << "  if(GBrowserIsCompatible()) {"
  js << "    #{dom_id} = new GMap2(document.getElementById('#{dom_id}'));"

  js << "    if (self['GoogleMapOnLoad']) {"
  js << "      #{dom_id}.load = GEvent.addListener(#{dom_id},'load',GoogleMapOnLoad)"
  js << "    }"

  js << '    ' + map_type_js
  js << '    ' + controls_js
  js << '    ' + center_on_bounds_js
  js << '    ' + markers_icons_js

  # Put all the markers on the map.
  for marker in markers
    js << '    ' + marker.to_js
    js << ''
  end

  overlays.each do |overlay|
    js << overlay.to_js
    js << "#{dom_id}.addOverlay(#{overlay.dom_id});"  
  end

  js << "#{dom_id}.#{to_enable_prefix double_click_zoom}DoubleClickZoom();"
  js << "#{dom_id}.#{to_enable_prefix continuous_zoom}ContinuousZoom();"
  js << "#{dom_id}.#{to_enable_prefix scroll_wheel_zoom}ScrollWheelZoom();"

  js << '    ' + inject_on_load.gsub("\n", "    \n") if inject_on_load
  js << "  }"
  js << "}"

  # Load the map on window load preserving anything already on window.onload.
  js << "if (typeof window.onload != 'function') {"
  js << "  window.onload = initialize_google_map_#{dom_id};"
  js << "} else {"
  js << "  old_before_google_map_#{dom_id} = window.onload;"
  js << "  window.onload = function() {" 
  js << "    old_before_google_map_#{dom_id}();"
  js << "    initialize_google_map_#{dom_id}();"
  js << "  }"
  js << "}"

  # Unload the map on window load preserving anything already on window.onunload.
  #js << "if (typeof window.onunload != 'function') {"
  #js << "  window.onunload = GUnload();"
  #js << "} else {"
  #js << "  old_before_onunload = window.onload;"
  #js << "  window.onunload = function() {" 
  #js << "    old_before_onunload;"
  #js << "    GUnload();" 
  #js << "  }"
  #js << "}"

  return js.join("\n")
end