| Class | Ym4r::GmPlugin::GMap |
| In: |
lib/gm_plugin/map.rb
|
| Parent: | Object |
Representing the Google Maps API class GMap2.
| VML_NAMESPACE | = | "xmlns:v=\"urn:schemas-microsoft-com:vml\"" | A constant containing the declaration of the VML namespace, necessary to display polylines under IE. |
| container | [R] | The id of the DIV that will contain the map in the HTML page. |
Outputs the header necessary to use the Google Maps API, by including the JS files of the API, as well as a file containing YM4R/GM helper functions. By default, it also outputs a style declaration for VML elements. This default can be overriddent by passing :with_vml => false as option to the method. You can also pass a :host option in order to select the correct API key for the location where your app is currently running, in case the current environment has multiple possible keys. Usually, in this case, you should pass it @request.host. If you have defined only one API key for the current environment, the :host option is ignored. Finally you can override all the key settings in the configuration by passing a value to the :key key. Finally, you can pass a language for the map type buttons with the :hl option (possible values are: Japanese (ja), French (fr), German (de), Italian (it), Spanish (es), Catalan (ca), Basque (eu) and Galician (gl): no values means english)
# File lib/gm_plugin/map.rb, line 29
29: def self.header(options = {})
30: options[:with_vml] = true unless options.has_key?(:with_vml)
31: options[:hl] ||= ''
32: api_key = ApiKey.get(options)
33: a = "<script src=\"http://maps.google.com/maps?file=api&v=2&key=#{api_key}&hl=#{options[:hl]}\" type=\"text/javascript\"></script>\n"
34: a << "<script src=\"/javascripts/ym4r-gm.js\" type=\"text/javascript\"></script>\n"
35: a << "<style type=\"text/css\">\n v\:* { behavior:url(#default#VML);}\n</style>" if options[:with_vml]
36: a
37: end
By default the map in the HTML page will be globally accessible with the name map.
# File lib/gm_plugin/map.rb, line 14
14: def initialize(container, variable = "map")
15: @container = container
16: @variable = variable
17: @init = []
18: @init_end = [] #for stuff that must be initialized at the end (controls)
19: @init_begin = [] #for stuff that must be initialized at the beginning (center + zoom)
20: @global_init = []
21: end
Sets up a new map type. If add is false, all the other map types of the map are wiped out. If you want to access the map type in other methods, you should declare the map type first (with declare_init).
# File lib/gm_plugin/map.rb, line 139
139: def add_map_type_init(map_type, add = true)
140: unless add
141: @init << get_map_types.set_property(:length,0)
142: end
143: @init << add_map_type(map_type)
144: end
Initializes the initial center and zoom of the map. center can be both a GLatLng object or a 2-float array.
# File lib/gm_plugin/map.rb, line 101
101: def center_zoom_init(center, zoom)
102: if center.is_a?(GLatLng)
103: @init_begin << set_center(center,zoom)
104: else
105: @init_begin << set_center(GLatLng.new(center),zoom)
106: end
107: end
Center and zoom based on the bbox corners. Pass a GLatLngBounds object, an array of 2D coordinates (sw and ne) or an array of GLatLng objects (sw and ne).
# File lib/gm_plugin/map.rb, line 120
120: def center_zoom_on_bounds_init(latlngbounds)
121: if(latlngbounds.is_a?(Array))
122: if latlngbounds[0].is_a?(Array)
123: latlngbounds = GLatLngBounds.new(GLatLng.new(latlngbounds[0]),GLatLng.new(latlngbounds[1]))
124: elsif latlngbounds[0].is_a?(GLatLng)
125: latlngbounds = GLatLngBounds.new(*latlngbounds)
126: end
127: end
128: #else it is already a latlngbounds object
129:
130: @init_begin << center_and_zoom_on_bounds(latlngbounds)
131: end
Center and zoom based on the coordinates passed as argument (either 2D arrays or GLatLng objects)
# File lib/gm_plugin/map.rb, line 110
110: def center_zoom_on_points_init(*points)
111: if(points.length > 0)
112: if(points[0].is_a?(Array))
113: points = points.collect { |point| GLatLng.new(point) }
114: end
115: @init_begin << center_and_zoom_on_points(points)
116: end
117: end
Initializes the controls: you can pass a hash with keys :small_map, :large_map, :small_zoom, :scale, :map_type and :overview_map and a boolean value as the value (usually true, since the control is not displayed by default)
# File lib/gm_plugin/map.rb, line 59
59: def control_init(controls = {})
60: @init_end << add_control(GSmallMapControl.new) if controls[:small_map]
61: @init_end << add_control(GLargeMapControl.new) if controls[:large_map]
62: @init_end << add_control(GSmallZoomControl.new) if controls[:small_zoom]
63: @init_end << add_control(GScaleControl.new) if controls[:scale]
64: @init_end << add_control(GMapTypeControl.new) if controls[:map_type]
65: @init_end << add_control(GOverviewMapControl.new) if controls[:overview_map]
66: end
Outputs in JavaScript the creation of a GMap2 object
# File lib/gm_plugin/map.rb, line 236
236: def create
237: "new GMap2(document.getElementById(\"#{@container}\"))"
238: end
Globally declare a MappingObject with variable name "name"
# File lib/gm_plugin/map.rb, line 190
190: def declare_global_init(variable,name)
191: @global_init << variable.declare(name)
192: end
Locally declare a MappingObject with variable name "name"
# File lib/gm_plugin/map.rb, line 154
154: def declare_init(variable, name)
155: @init << variable.declare(name)
156: end
Outputs the <div id=…></div> which has been configured to contain the map. You can pass :width and :height as options to output this in the style attribute of the DIV element (you could also achieve the same effect by putting the dimension info into a CSS or using the instance method GMap#header_width_height)
# File lib/gm_plugin/map.rb, line 40
40: def div(options = {})
41: attributes = "id=\"#{@container}\" "
42: if options.has_key?(:height) && options.has_key?(:width)
43: attributes += "style=\"width:#{options[:width]}px;height:#{options[:height]}px\""
44: end
45: "<div #{attributes}></div>"
46: end
Registers an event globally
# File lib/gm_plugin/map.rb, line 179
179: def event_global_init(object,event,callback)
180: @global_init << "GEvent.addListener(#{object.to_javascript},\"#{MappingObject.javascriptify_method(event.to_s)}\",#{callback});"
181: end
Registers an event
# File lib/gm_plugin/map.rb, line 174
174: def event_init(object,event,callback)
175: @init << "GEvent.addListener(#{object.to_javascript},\"#{MappingObject.javascriptify_method(event.to_s)}\",#{callback});"
176: end
Deprecated. Use the static version instead.
# File lib/gm_plugin/map.rb, line 24
24: def header(with_vml = true)
25: GMap.header(:with_vml => with_vml)
26: end
Outputs a style declaration setting the dimensions of the DIV container of the map. This info can also be set manually in a CSS.
# File lib/gm_plugin/map.rb, line 49
49: def header_width_height(width,height)
50: "<style type=\"text/css\">\n##{@container} { height: #{height}px;\n width: #{width}px;\n}\n</style>"
51: end
Initializes an icon and makes it globally accessible through the JavaScript variable of name variable.
# File lib/gm_plugin/map.rb, line 169
169: def icon_global_init(icon , name)
170: declare_global_init(icon,name)
171: end
Deprecated. Use icon_global_init instead.
# File lib/gm_plugin/map.rb, line 164
164: def icon_init(icon , name)
165: icon_global_init(icon , name)
166: end
Initializes the interface configuration: double-click zoom, dragging, continuous zoom,… You can pass a hash with keys :dragging, :info_window, :double_click_zoom, :continuous_zoom. The values should be true or false. Check the google maps API doc to know what the default values are.
# File lib/gm_plugin/map.rb, line 69
69: def interface_init(interface = {})
70: if !interface[:dragging].nil?
71: if interface[:dragging]
72: @init << enableDragging()
73: else
74: @init << disableDragging()
75: end
76: end
77: if !interface[:info_window].nil?
78: if interface[:info_window]
79: @init << enableInfoWindow()
80: else
81: @init << disableInfoWindow()
82: end
83: end
84: if !interface[:double_click_zoom].nil?
85: if interface[:double_click_zoom]
86: @init << enableDoubleClickZoom()
87: else
88: @init << disableDoubleClickZoom()
89: end
90: end
91: if !interface[:continuous_zoom].nil?
92: if interface[:continuous_zoom]
93: @init << enableContinuousZoom()
94: else
95: @init << disableContinuousZoom()
96: end
97: end
98: end
Declares the overlay globally with name name
# File lib/gm_plugin/map.rb, line 184
184: def overlay_global_init(overlay,name)
185: declare_global_init(overlay,name)
186: @init << add_overlay(overlay)
187: end
Initializes the map by adding an overlay (marker or polyline).
# File lib/gm_plugin/map.rb, line 134
134: def overlay_init(overlay)
135: @init << add_overlay(overlay)
136: end
Records arbitrary JavaScript code and outputs it during initialization outside the load function (ie globally).
# File lib/gm_plugin/map.rb, line 159
159: def record_global_init(code)
160: @global_init << code
161: end
Records arbitrary JavaScript code and outputs it during initialization inside the load function.
# File lib/gm_plugin/map.rb, line 54
54: def record_init(code)
55: @init << code
56: end
Sets the map type displayed by default after the map is loaded. It should be known from the map (ie either the default map types or a user-defined map type added with add_map_type_init). Use set_map_type_init(GMapType::G_SATELLITE_MAP) or set_map_type_init(GMapType::G_HYBRID_MAP) to initialize the map with repsecitvely the Satellite view and the hybrid view.
# File lib/gm_plugin/map.rb, line 149
149: def set_map_type_init(map_type)
150: @init << set_map_type(map_type)
151: end
Outputs the initialization code for the map. By default, it outputs the script tags, performs the initialization in response to the onload event of the window and makes the map globally available. If you pass true to the option key :full, the map will be setup in full screen, in which case it is not necessary (but not harmful) to set a size for the map div.
# File lib/gm_plugin/map.rb, line 195
195: def to_html(options = {})
196: no_load = options[:no_load]
197: no_script_tag = options[:no_script_tag]
198: no_declare = options[:no_declare]
199: no_global = options[:no_global]
200: fullscreen = options[:full]
201:
202: html = ""
203: html << "<script type=\"text/javascript\">\n" if !no_script_tag
204: #put the functions in a separate javascript file to be included in the page
205: html << @global_init * "\n"
206: html << "var #{@variable};\n" if !no_declare and !no_global
207: html << "window.onload = addCodeToFunction(window.onload,function() {\nif (GBrowserIsCompatible()) {\n" if !no_load
208:
209: if fullscreen
210: #Adding the initial resizing and setting up the event handler for
211: #future resizes
212: html << "setWindowDims(document.getElementById('#{@container}'));\n"
213: html << "if (window.attachEvent) { window.attachEvent(\"onresize\", function() {setWindowDims(document.getElementById('#{@container}'));})} else {window.addEventListener(\"resize\", function() {setWindowDims(document.getElementById('#{@container}')); } , false);}\n"
214: end
215:
216: if !no_declare and no_global
217: html << "#{declare(@variable)}\n"
218: else
219: html << "#{assign_to(@variable)}\n"
220: end
221: html << @init_begin * "\n"
222: html << @init * "\n"
223: html << @init_end * "\n"
224: html << "\n}\n});\n" if !no_load
225: html << "</script>" if !no_script_tag
226:
227: if fullscreen
228: #setting up the style in case of full screen
229: html << "<style>html, body {width: 100%; height: 100%} body {margin-top: 0px; margin-right: 0px; margin-left: 0px; margin-bottom: 0px} ##{@container} {margin: 0px;} </style>"
230: end
231:
232: html
233: end