| Class | Ym4r::MapstractionPlugin::Mapstraction |
| In: |
lib/mapstraction_plugin/mapstraction.rb
|
| Parent: | Object |
Represents a Mapstracted map.
| VML_NAMESPACE | = | "xmlns:v=\"urn:schemas-microsoft-com:vml\"" | A constant containing the declaration of the VML namespace, necessary to display polylines in google maps under IE. |
| container | [R] | The id of the DIV that will contain the map in the HTML page. |
| map_type | [R] | The id of the DIV that will contain the map in the HTML page. |
# File lib/mapstraction_plugin/mapstraction.rb, line 32
32: def self.header(types,options = {})
33: a = ""
34: Array(types).each do |type|
35: if type == :google
36: options[:with_vml] = true unless options.has_key?(:with_vml)
37: if options.has_key?(:key)
38: api_key = options[:key]
39: elsif GMAPS_API_KEY.is_a?(Hash)
40: #For this environment, multiple hosts are possible.
41: #:host must have been passed as option
42: if options.has_key?(:host)
43: api_key = GMAPS_API_KEY[options[:host]]
44: else
45: raise AmbiguousGMapsAPIKeyException.new(GMAPS_API_KEY.keys.join(","))
46: end
47: else
48: #Only one possible key: take it
49: api_key = GMAPS_API_KEY
50: end
51: a << "<script src=\"http://maps.google.com/maps?file=api&v=2&key=#{api_key}\" type=\"text/javascript\"></script>\n"
52: a << "<style type=\"text/css\">\n v\:* { behavior:url(#default#VML);}\n</style>" if options[:with_vml]
53: elsif type == :yahoo
54: a << "<script type=\"text/javascript\" src=\"http://api.maps.yahoo.com/ajaxymap?v=3.4&appid=YellowMasp4R\"></script>\n"
55: elsif type == :microsoft
56: a << "<script src=\"http://dev.virtualearth.net/mapcontrol/v3/mapcontrol.js\"></script>\n"
57: end
58: end
59: a << "<script src=\"/javascripts/mapstraction.js\" type=\"text/javascript\"></script>\n"
60: a << "<script src=\"/javascripts/ym4r-mapstraction.js\" type=\"text/javascript\"></script>\n"
61: a
62: end
By default the map in the HTML page will be globally accessible with the name map. map_type is one of three choices: :google, :yahoo or microsoft.
# File lib/mapstraction_plugin/mapstraction.rb, line 22
22: def initialize(container, map_type, variable = "map")
23: @container = container
24: @map_type = map_type
25: @variable = variable
26: @init = []
27: @init_begin = []
28: @init_end = []
29: @global_init = []
30: end
Initializes the initial center and zoom of the map. center can be both a GLatLng object or a 2-float array.
# File lib/mapstraction_plugin/mapstraction.rb, line 95
95: def center_zoom_init(center, zoom)
96: if center.is_a?(LatLonPoint)
97: @init_begin << set_center_and_zoom(center,zoom)
98: else
99: @init_begin << set_center_and_zoom(LatLonPoint.new(center),zoom)
100: end
101: 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/mapstraction_plugin/mapstraction.rb, line 104
104: def center_zoom_on_bounds_init(bounds)
105: if bounds.is_a?(Array)
106: if bounds[0].is_a?(Array)
107: bounds = BoundingBox.new(bounds[0][0],bounds[0][1],bounds[1][0],bounds[1][1])
108: elsif bounds[0].is_a?(LatLonPoint)
109: bounds = BoundingBox.new(bounds[0].lat,bounds[0].lon,bounds[1].lat,bounds[1].lon)
110: end
111: end
112: #else it is already a latlngbounds object
113:
114: @init_begin << set_bounds(bounds)
115: end
# File lib/mapstraction_plugin/mapstraction.rb, line 175
175: def clusterer_global_init(clusterer,name)
176: declare_global_init(clusterer,name)
177: clusterer_init(clusterer)
178: end
# File lib/mapstraction_plugin/mapstraction.rb, line 130
130: def clusterer_init(clusterer)
131: @init << add_clusterer(clusterer)
132: end
Initializes the controls: you can pass a hash with key :small (only one for now) and a boolean value as the value (usually true, since the control is not displayed by default). Also in later version, you can be a bit more precise, with the following keys: pan (boolean), zoom (:large or :small), overview (boolean), scale (boolean), map_type (boolean)
# File lib/mapstraction_plugin/mapstraction.rb, line 84
84: def control_init(controls = {})
85: if controls[:small]
86: @init << add_small_controls()
87: elsif controls[:large]
88: @init << add_large_controls()
89: else
90: @init << add_controls(controls)
91: end
92: end
Outputs in JavaScript the creation of a Mapstraction object
# File lib/mapstraction_plugin/mapstraction.rb, line 233
233: def create
234: "new Mapstraction(\"#{@container}\",\"#{@map_type.to_s}\")"
235: end
Globally declare a MappingObject with variable name "name"
# File lib/mapstraction_plugin/mapstraction.rb, line 186
186: def declare_global_init(variable,name)
187: @global_init << variable.declare(name)
188: end
Locally declare a MappingObject with variable name "name"
# File lib/mapstraction_plugin/mapstraction.rb, line 144
144: def declare_init(variable, name)
145: @init << variable.declare(name)
146: 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 Mapstraction#header_width_height)
# File lib/mapstraction_plugin/mapstraction.rb, line 65
65: def div(options = {})
66: attributes = "id=\"#{@container}\" "
67: if options.has_key?(:height) && options.has_key?(:width)
68: attributes += "style=\"width:#{options[:width]}px;height:#{options[:height]}px\""
69: end
70: "<div #{attributes}></div>"
71: end
Registers an event globally
# File lib/mapstraction_plugin/mapstraction.rb, line 159
159: def event_global_init(object,event,callback)
160: @global_init << "#{object.to_javascript}.addEventListener(\"#{MappingObject.javascriptify_method(event.to_s)}\",#{callback})"
161: end
Registers an event
# File lib/mapstraction_plugin/mapstraction.rb, line 154
154: def event_init(object,event,callback)
155: @init << "#{object.to_javascript}.addEventListener(\"#{MappingObject.javascriptify_method(event.to_s)}\",#{callback})"
156: 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/mapstraction_plugin/mapstraction.rb, line 74
74: def header_width_height(width,height)
75: "<style type=\"text/css\">\n##{@container} { height: #{height}px;\n width: #{width}px;\n}\n</style>"
76: end
Declares the marker globally with name name
# File lib/mapstraction_plugin/mapstraction.rb, line 164
164: def marker_global_init(marker,name, options = {})
165: declare_global_init(marker,name)
166: marker_init(marker,options)
167: end
Declares the marker group globally with name name
# File lib/mapstraction_plugin/mapstraction.rb, line 170
170: def marker_group_global_init(marker_group,name)
171: declare_global_init(marker_group,name)
172: marker_group_init(marker_group)
173: end
# File lib/mapstraction_plugin/mapstraction.rb, line 126
126: def marker_group_init(marker_group)
127: @init << add_marker_group(marker_group)
128: end
Initializes the map by adding a marker
# File lib/mapstraction_plugin/mapstraction.rb, line 118
118: def marker_init(marker, options = {})
119: if options[:open_bubble]
120: @init << add_marker_and_open(marker)
121: else
122: @init << add_marker(marker)
123: end
124: end
# File lib/mapstraction_plugin/mapstraction.rb, line 180
180: def polyline_global_init(polyline, name)
181: declare_global_init(polyline,name)
182: polyline_init(clusterer)
183: end
# File lib/mapstraction_plugin/mapstraction.rb, line 134
134: def polyline_init(polyline)
135: @init << add_polyline(polyline)
136: end
Records arbitrary JavaScript code and outputs it during initialization outside the load function (ie globally).
# File lib/mapstraction_plugin/mapstraction.rb, line 149
149: def record_global_init(code)
150: @global_init << code
151: end
Records arbitrary JavaScript code and outputs it during initialization inside the load function.
# File lib/mapstraction_plugin/mapstraction.rb, line 79
79: def record_init(code)
80: @init << code
81: end
Sets the map type displayed by default after the map is loaded.
# File lib/mapstraction_plugin/mapstraction.rb, line 139
139: def set_map_type_init(map_type)
140: @init << set_map_type(map_type)
141: 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. You can pass the :full => true option to setup fullscreen for the map.
# File lib/mapstraction_plugin/mapstraction.rb, line 191
191: def to_html(options = {})
192: no_load = options[:no_load]
193: no_script_tag = options[:no_script_tag]
194: no_declare = options[:no_declare]
195: no_global = options[:no_global]
196: fullscreen = options[:full]
197:
198: html = ""
199: html << "<script type=\"text/javascript\">\n" if !no_script_tag
200: #put the functions in a separate javascript file to be included in the page
201: html << @global_init * "\n"
202: html << "var #{@variable};\n" if !no_declare and !no_global
203: html << "window.onload = addCodeToFunction(window.onload,function() {\n" if !no_load
204:
205: if fullscreen
206: #Adding the initial resizing and setting up the event handler for
207: #future resizes
208: html << "setWindowDims($('#{@container}'));\n"
209: html << "if (window.attachEvent) { window.attachEvent(\"onresize\", function() {setWindowDims($('#{@container}'));})} else {window.addEventListener(\"resize\", function() {setWindowDims($('#{@container}')); } , false);}\n"
210: end
211:
212: if !no_declare and no_global
213: html << "#{declare(@variable)}\n"
214: else
215: html << "#{assign_to(@variable)}\n"
216: end
217:
218: html << @init_begin * "\n"
219: html << @init * "\n"
220: html << @init_end * "\n"
221: html << "\n});\n" if !no_load
222: html << "</script>" if !no_script_tag
223:
224: if fullscreen
225: #setting up the style in case of full screen
226: html << "<style>html, body {width: 100%; height: 100%} body {margin:0} ##{@container} {margin:0} </style>"
227: end
228:
229: html
230: end