Class: LeafletHelper::L

Inherits:
Object
  • Object
show all
Defined in:
lib/leaflet_helper/l.rb

Constant Summary collapse

VERSION =

of leaflet.js

'0.7.7'
JS =
"http://cdn.leafletjs.com/leaflet/v#{VERSION}/leaflet.js"
CSS =
"http://cdn.leafletjs.com/leaflet/v#{VERSION}/leaflet.css"
MarkerClusterJS =
"https://raw.githubusercontent.com/Leaflet/Leaflet.markercluster/leaflet-0.7/dist/leaflet.markercluster.js"
MarkerClusterCSS =
"https://raw.githubusercontent.com/Leaflet/Leaflet.markercluster/leaflet-0.7/dist/MarkerCluster.css"
@@defaults =

FIXME: Needs to be isolated between maps in a multi-map application.

experiments/maps shows the problem
{
  leaflet_helper_base:
  {   # the options from L#init
    plugins:        [],
    latitude:       AREA51.latitude,
    longitude:      AREA51.longitude,
    zoom:           9,
    min_zoom:       2,
    max_zoom:       22
  }
}

Class Method Summary collapse

Class Method Details

.add_mapbox_layer(id = "map", options = {}) ⇒ Object

NOTE: To use mapbox you must have an account



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/leaflet_helper/l.rb', line 121

def add_mapbox_layer(id="map", options={})
  @@defaults[id] = {
              id:                   id,
              map_name:             get_map_name(id),
              mapbox:               true,
              mapbox_url:           ENV['MAPBOX_URL']           || "https://api.mapbox.com/styles/v1/{mbUser}/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}",
              mapbox_user:          ENV['MAPBOX_USER']          || "your.mapbox.user.account",
              mapbox_style_id:      ENV['MAPBOX_STYLE_ID']      || "your.mapbox.project.id",
              mapbox_access_token:  ENV['MAPBOX_ACCESS_TOKEN']  || "your.mapbox.access.token",
              mapbox_attribution:   'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>',
            }.merge(options)

  o = @@defaults[:leaflet_helper_base].merge( @@defaults[id] )

  return U.pull_in 'add_mapbox_layer.js.erb', o
end

.add_openstreetmap_layer(id = "map", options = {}) ⇒ Object

The default tile provider is Open Street Map project



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/leaflet_helper/l.rb', line 105

def add_openstreetmap_layer(id="map", options={})
  @@defaults[id] = {
              id:               id,
              map_name:         get_map_name(id),
              openstreetmap:    true,
              osm_url:          'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
              osm_attribution:  'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors'
            }.merge(options)

  o = @@defaults[:leaflet_helper_base].merge( @@defaults[id] )

  return U.pull_in 'add_openstreetmap_layer.js.erb', o
end

.add_support_for_markers(id = "map", options = {}) ⇒ Object

Allows for the generation of markers on top of the map



140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/leaflet_helper/l.rb', line 140

def add_support_for_markers(id="map", options={})
  @@defaults[id] = {
              id:         id,
              map_name:   get_map_name(id),
              route:      "#{id}/markers",
              markers:    true,
              cluster:    false
            }.merge(options)

  o = @@defaults[:leaflet_helper_base].merge( @@defaults[id] )

  return U.pull_in 'marker_support.js.erb', o
end

.defaults(id) ⇒ Object

LeafletHelper::U needs access to the L’s class variable



163
164
165
# File 'lib/leaflet_helper/l.rb', line 163

def defaults(id)
  @@defaults[id]
end

.get_map_name(id = "map") ⇒ Object

The Javascript container that has the map for this id



156
157
158
# File 'lib/leaflet_helper/l.rb', line 156

def get_map_name(id="map")
  return "lh_#{id}"
end

.init(options = {}) ⇒ Object

intended for the the head section of a web page



31
32
33
34
35
36
37
38
39
40
# File 'lib/leaflet_helper/l.rb', line 31

def init(options={})
  @@defaults[:leaflet_helper_base].merge! options

  if  @@defaults[:leaflet_helper_base][:plugins].include?(:cluster)  &&
      @@defaults[:leaflet_helper_base][:cluster_marker].nil?
    @@defaults[:leaflet_helper_base][:cluster_marker] = U.pull_in('cluster_icon_create_function.js.erb')
  end

  return U.pull_in 'head.html.erb', @@defaults[:leaflet_helper_base]
end

.place_map_here(id = 'map', options = {}) ⇒ Object

intended for the body of a web page can support multiple maps on a page; each map must have a unique id



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/leaflet_helper/l.rb', line 46

def place_map_here(id='map', options={})
  o = {
        style: "width: 1200px; height: 400px"
      }.merge(options)

  div_options = ""

  o.each_pair do |k,v|
    div_options += " #{k.to_s}=" + '"' + v + '"'
  end

  return U.pull_in 'map_div.html.erb', {id: id, parameters: div_options}
end

.set_view(id = 'map', options = {}) ⇒ Object

Center an existing map to a specific location



92
93
94
95
96
97
98
99
100
101
# File 'lib/leaflet_helper/l.rb', line 92

def set_view(id='map', options={})
  @@defaults[id] = {
              id:         id,
              map_name:   get_map_name(id)
            }.merge(options)

  o = @@defaults[:leaflet_helper_base].merge( @@defaults[id] )

  return U.pull_in 'set_view.js.erb', o
end

.show_map(id = "map", options = {}) ⇒ Object

Intended for the body at the bottom see the example for, er uh, an example



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
# File 'lib/leaflet_helper/l.rb', line 63

def show_map(id="map", options={})
  @@defaults[id] = {
              id:         id,
              map_name:   get_map_name(id),
              markers:    false,
              cluster:    false
            }.merge(options)

  o = @@defaults[:leaflet_helper_base].merge( @@defaults[id] )

  script = "<script>\n"
  script += U.pull_in 'show_map.js.erb', o
  script += set_view(id, o)

  # SMELL: why can't you have both OSM and Mapbox?
  #        If I renember right there is a way within Mapbox Studio to incorporate
  #        an open street map layer.  There is also this:
  #        http://gis.stackexchange.com/questions/116205/multiple-simultaneous-tilelayers-in-leaflet
  script += add_openstreetmap_layer(id, o)  if o[:openstreetmap] && !o[:mapbox]
  script += add_mapbox_layer(id, o)         if o[:mapbox]        && !o[:openstreetmap]
  script += add_support_for_markers(id, o)  if o[:markers]

  script += "</script>\n"

  return script
end