aboutsummaryrefslogtreecommitdiffstats
path: root/app/maps/application_map.rb
blob: d7ee0b5a1c58d73e057a728f3810b74188d9608a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
class ApplicationMap

  include MapLayers
  include MapLayers::ViewHelpers
  #delegate :url_helpers, :to => :'Rails.application.routes' 
  include Rails.application.routes.url_helpers
  
  def projection(name)
    OpenLayers::Projection.new(name)
  end

  def controls
    [ OpenLayers::Control::LayerSwitcher.new(:ascending => true),
      OpenLayers::Control::ScaleLine.new,
      OpenLayers::Control::Navigation.new,
      OpenLayers::Control::PanZoomBar.new,
      OpenLayers::Control::Attribution.new]
  end

  def id
    "map"
  end

  def map
    @map ||= MapLayers::Map.new(id, :projection => projection("EPSG:900913"), :controls => controls)
  end

  def default_class
    self.class.name.underscore.gsub(/_map$/, '')
  end

  def name
    self.class.name.underscore.gsub(/_map$/, '')
  end

  def to_html(options = {})
    if not respond_to?(:ready?) or ready?
      "<div id=\"#{id}\" class=\"#{default_class}\"></div> #{map.to_html(options)}".html_safe
    end
  end

  def kml
    OpenLayers::Format::KML.new :extractStyles => true, :extractAttributes => true, :maxDepth => 2
  end

  def strategy_fixed
    OpenLayers::Strategy::Fixed.new :preload => true
  end

  def google_physical
    OpenLayers::Layer::Google.new("Google Physical",
                      :type => :"google.maps.MapTypeId.TERRAIN")
  end

  def google_streets
    OpenLayers::Layer::Google.new "Google Streets", :numZoomLevels => 20
  end

  def google_hybrid
    OpenLayers::Layer::Google.new "Google Hybrid", :type => :"google.maps.MapTypeId.HYBRID", :numZoomLevels => 20
  end
  
  def google_satellite
    OpenLayers::Layer::Google.new "Google Satellite", :type => :"google.maps.MapTypeId.SATELLITE", :numZoomLevels => 22
  end

  def hover_control_display_name(layer)
    OpenLayers::Control::SelectFeature.new( layer, { 
                                              :autoActivate => true,
                                              :hover => true,
                                              :renderIntent => "temporary",
                                              :eventListeners => {
                                                :featurehighlighted => JsExpr.new("function(e) {
          feature = e.feature ;
          popup = new OpenLayers.Popup.AnchoredBubble('chicken', 
                                                 new OpenLayers.LonLat(feature.geometry.x, feature.geometry.y),
                                                 null,
                                                 \"<div class='popup_hover'><b>\" + feature.attributes.name +\"</b></div> \", null, false, null);
          popup.autoSize = true;
          popup.displayClass = 'popup_hover';

          feature.popup = popup;
          map.addPopup(popup);
        }"),
                                                :featureunhighlighted => JsExpr.new("function(e) {
          feature = e.feature;
          map.removePopup(feature.popup);
          feature.popup.destroy();
          feature.popup = null;  
        }")
                                              } } )
  end

  def kml_layer(url, options = {})   
    url = polymorphic_path([url.referential, url], :format => :kml) unless String === url 
    protocol = OpenLayers::Protocol::HTTP.new :url => url, :format => kml
    OpenLayers::Layer::Vector.new name, {:projection => projection("EPSG:4326"), :strategies => [strategy_fixed], :protocol => protocol, :displayInLayerSwitcher => false}.merge(options)
  end

end