Class: Ropenlayer::Openlayer::Map

Inherits:
Object
  • Object
show all
Defined in:
lib/ropenlayer/openlayer/map.rb

Constant Summary collapse

MAP_JS_PREFIX =

used to scope js variables created with the map. Js name will be created with this prefix + plus dom_id argument

'olObject'
DEFAULT_ADD_FEATURE_CALLBACK =
'addFeatureToMap'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dom_id, options = {}) ⇒ Map

Initialize

Create a map object with parsed attributes. Ropenlayer manage two ids for each map.

  • js_div, use to scope all the variables names to draw js output.

  • div_id, the dom_id passed as argument to construtor. For example

For example

m = Ropenlayer::Openlayer::Map.new('group_map')
=> <Ropenlayer::Openlayer::Map:0xf7344fe8>
m.js_id
=> "olObject_group_map"
n.div_id
=> "group_map"

To draw map, just call to_js method. For example, in a rails 3.x application

<%= raw javascript_tag(Ropenlayer::Openlayer::Map.new('group_map').to_js) %>

Ropenlayer::Openlayer::Map use this options

  • layers: Select which layers are used in map. For a list of available layers see Ropenlayer::Openlayer::Laye::Baser

  • default_layer: Default layer at render. Default layers.first

  • layer_select: If true, map will render a control for change between differents layers

  • controls: Select which controls are used in map. For a list of available controlse see Ropenlayer::Openlayer::Control

  • latitude: Specify default latitude for map. Map will be pointed at this latitude at render.

  • longitude: Specify default longitude for map. Map will be pointed at this longitude at render.

  • zoom: Specify default zoom for map. Map will be render with this zoom enable.

  • edit_map: If true, map will display a custom control what enable edit capabilities with map

Map object accepts other options scoped in :map_js_options key.

  • theme: CSS theme used to display. By default none is used

  • projection: Specify projection. Argument must be a valid OpenLayers.Projection JS object. See more on

  • units: Units for measure. By default: “‘m’”

  • maxResolution: maxResolution of the map, by default word length: “156543.0339”

  • meaxExtentd: Bound for projection. Must be valid OpenLayers.Bounds object. By default world length bound. See for more info.

After display objects, you can, ofc, call methods from openlayers api to instace js variable. For example

<% map = Ropenlayer::Openlayer::Map.new('group_map') %>
<%= raw javascript_tag map.to_js %>
<%= raw javascript_tag "#{ map.js_id }.setCenter(new OpenLayers.LonLat(0,0), 10);" %>

Will call the setCenter OpenLayer.Map JS method to map.js_id js instace object



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
112
113
114
115
116
117
# File 'lib/ropenlayer/openlayer/map.rb', line 80

def initialize(dom_id, options = {})
  @js_id          = "#{ MAP_JS_PREFIX  }_#{ dom_id }"
  @div_id         = "#{ dom_id }"
  
  options[:add_feature_callback] ||= DEFAULT_ADD_FEATURE_CALLBACK
  @options        = options
  
  @js_propierties = map_js_propierties(options[:map_js_options])
  
  @layer_names    = @options[:layers]   || self.class.default_layers
  
  if options[:default_layer]
    def_layer    = @layer_names.delete(options[:default_layer])
    @layer_names =  @layer_names.push(def_layer).reverse if def_layer
  end
  
  @layers         = Ropenlayer::Openlayer::Layer::Base.build_collection(self)
  
  @control_names  = @options[:controls] || self.class.default_controls
  @controls       = Ropenlayer::Openlayer::Control.build_collection(self)
  
  @latitude       = @options[:latitude]  || self.class.default_latitude
  @longitude      = @options[:longitude] || self.class.default_longitude
  @zoom           = @options[:zoom]      || self.class.default_zoom
  
  @layers_select  = @options[:layer_select].nil? or @options[:layer_select]
  @edit_map       = @options[:edit_map]
  
  @markers_layer  =  Ropenlayer::Openlayer::Layer::ElementsMarker.new(self)
  @vectors_layer  =  Ropenlayer::Openlayer::Layer::ElementsVector.new(self)
  
  @draw_features  = options[:draw_features] || []

  @js_notification_area      = "#{ @js_id }_notification_area"
  @form_localizations_fields = {}
  
  @js_helper      = Ropenlayer.js_helper
end

Instance Attribute Details

#control_namesObject

Returns the value of attribute control_names.



17
18
19
# File 'lib/ropenlayer/openlayer/map.rb', line 17

def control_names
  @control_names
end

#controlsObject

Returns the value of attribute controls.



18
19
20
# File 'lib/ropenlayer/openlayer/map.rb', line 18

def controls
  @controls
end

#div_idObject (readonly)

Returns the value of attribute div_id.



6
7
8
# File 'lib/ropenlayer/openlayer/map.rb', line 6

def div_id
  @div_id
end

#draw_featuresObject

Returns the value of attribute draw_features.



26
27
28
# File 'lib/ropenlayer/openlayer/map.rb', line 26

def draw_features
  @draw_features
end

#form_localizations_fieldsObject

Returns the value of attribute form_localizations_fields.



24
25
26
# File 'lib/ropenlayer/openlayer/map.rb', line 24

def form_localizations_fields
  @form_localizations_fields
end

#js_helperObject (readonly)

Returns the value of attribute js_helper.



13
14
15
# File 'lib/ropenlayer/openlayer/map.rb', line 13

def js_helper
  @js_helper
end

#js_idObject (readonly)

Returns the value of attribute js_id.



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

def js_id
  @js_id
end

#js_notification_areaObject

Returns the value of attribute js_notification_area.



22
23
24
# File 'lib/ropenlayer/openlayer/map.rb', line 22

def js_notification_area
  @js_notification_area
end

#js_propiertiesObject (readonly)

Returns the value of attribute js_propierties.



7
8
9
# File 'lib/ropenlayer/openlayer/map.rb', line 7

def js_propierties
  @js_propierties
end

#layer_namesObject (readonly)

Returns the value of attribute layer_names.



10
11
12
# File 'lib/ropenlayer/openlayer/map.rb', line 10

def layer_names
  @layer_names
end

#layersObject (readonly)

Returns the value of attribute layers.



11
12
13
# File 'lib/ropenlayer/openlayer/map.rb', line 11

def layers
  @layers
end

#layers_selectObject

Returns the value of attribute layers_select.



15
16
17
# File 'lib/ropenlayer/openlayer/map.rb', line 15

def layers_select
  @layers_select
end

#markers_layerObject

Returns the value of attribute markers_layer.



20
21
22
# File 'lib/ropenlayer/openlayer/map.rb', line 20

def markers_layer
  @markers_layer
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/ropenlayer/openlayer/map.rb', line 8

def options
  @options
end

#vectors_layerObject

Returns the value of attribute vectors_layer.



21
22
23
# File 'lib/ropenlayer/openlayer/map.rb', line 21

def vectors_layer
  @vectors_layer
end

Class Method Details

.default_controlsObject



167
168
169
# File 'lib/ropenlayer/openlayer/map.rb', line 167

def self.default_controls
  [ :scale_line, :mouse_position, :pan_panel, :navigation, :zoom_panel  ]
end

.default_latitudeObject



155
156
157
# File 'lib/ropenlayer/openlayer/map.rb', line 155

def self.default_latitude
  4580313.35287
end

.default_layersObject



151
152
153
# File 'lib/ropenlayer/openlayer/map.rb', line 151

def self.default_layers
  [ :google_hybrid, :google_streets, :wms, :google_satellite, :google_physical, :osm ]
end

.default_longitudeObject



159
160
161
# File 'lib/ropenlayer/openlayer/map.rb', line 159

def self.default_longitude
  263274.20626803
end

.default_zoomObject



163
164
165
# File 'lib/ropenlayer/openlayer/map.rb', line 163

def self.default_zoom
  4
end

Instance Method Details

#add_notification_areaObject



142
143
144
145
146
147
148
# File 'lib/ropenlayer/openlayer/map.rb', line 142

def add_notification_area
  %(// Notification area
    #{ Ropenlayer::Openlayer::Js.new_var(@js_notification_area, Ropenlayer::Openlayer::Js.new(@js_helper.create_xhtml_element('div', { :class => "'olMapNotificationArea'",
                                                                                                                                       :style => "'display: none;'",
                                                                                                                                       :id    => "'#{ @js_notification_area }'" })).to_js) }
    )
end

#map_js_propierties(map_js_options = {}) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/ropenlayer/openlayer/map.rb', line 119

def map_js_propierties(map_js_options = {})
  map_js_options ||= {}
  propierties      = {}

  propierties[:div]            = "'#{ div_id }'"
  propierties[:controls]       = "[]"

  propierties[:theme]          =  map_js_options[:theme]          || "'/stylesheets/ropenlayer.css'"
  propierties[:projection]     =  map_js_options[:projection]     || "#{ Ropenlayer::Openlayer::Js.new_method("OpenLayers.Projection", :args => ["'EPSG:900913'"]) }"
  propierties[:units]          =  map_js_options[:units]          || "'m'"
  propierties[:eventListeners] =  map_js_options[:eventListeners] if  map_js_options[:eventListeners]

  propierties[:maxResolution]  =  map_js_options[:units]          || "156543.0339"
  propierties[:maxExtent]      =  map_js_options[:maxExtent]      || "#{ Ropenlayer::Openlayer::Js.new_method("OpenLayers.Bounds", :args => ["-20037508", "-20037508", "20037508", "20037508.34"]) }"
  propierties
end

#set_centerObject



137
138
139
140
# File 'lib/ropenlayer/openlayer/map.rb', line 137

def set_center
  %(// Setting center
    #{ Ropenlayer::Openlayer::Js.new("#{ @js_id }.setCenter(#{ Ropenlayer::Openlayer::Js.new_method("OpenLayers.LonLat", :args => [ @longitude, @latitude ]) }, #{ @zoom })").to_js } )
end

#to_jsObject

Map to JS OpenLayer construtor



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/ropenlayer/openlayer/map.rb', line 172

def to_js
  %( // Ropenlayer::Openlayer::Map.to_s OpenLayers Cartographic JS project licensed under BSD~style license. More information can be found at http://openlayers.org/
    #{ Ropenlayer::Openlayer::Js.new_var(@js_id, Ropenlayer::Openlayer::Js.new_method("OpenLayers.Map", :propierties => @js_propierties)).to_js } 
    
    #{ add_notification_area }
    
    #{ @layers.map(&:to_js) } 
    #{ @controls.map(&:to_js) } 
    
    #{ set_center } 
    
    #{ Ropenlayer::Openlayer::BoxControl::LayersSelect.new(self).to_js  if @layers_select }         
    #{ Ropenlayer::Openlayer::BoxControl::EditMap.new(self).to_js       if @edit_map }
        
    #{ @vectors_layer.to_js }
    #{ @markers_layer.to_js }
    
    #{ Ropenlayer::Openlayer::Feature.draw_collection(self)             if @draw_features.any? }   
  )
end