leaflet_helper 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 764b4f62f21a26bf252db2bbe2d4e3a790af0a73
4
- data.tar.gz: b0b0d1e5894155ae524d284c18e433362a012306
3
+ metadata.gz: fb2e318e079044796d4ead699979deea3b49bdae
4
+ data.tar.gz: d2a7a881a2824a0270180a5ef632010504f8825c
5
5
  SHA512:
6
- metadata.gz: f7d9e4ab4217d566e440a052d95b5b19786f81ec37e15230d13637d669dbd5e730ae4e587751db2688db997ca0add8c36479434a6816fb2be7c03a775e8251ee
7
- data.tar.gz: 9e7b350e260e63028f1178b34963115f751e7f9a648f6216f7fa98f26f24b91274e86917491a55a034ab9ab4bd9e06b97b102906f8b7f0ce699dc55b3e3dfdec
6
+ metadata.gz: 3cba373b4dd9394014eaa00d163b5631adcba322185ae89d04da101dc354b766bbdf9bc88ab418e9d40857cbed7abe5a5b7fc943e2110f9ddfaf4443cdaae56a
7
+ data.tar.gz: f6fc47ca34a34c6e980b6c514e19e4302aa22d55ca540faf268a73ce41ae90228ddb2480d5491bdc654612cf736193cbfa475e7102bb79e85d1787a602d1c269
@@ -5,7 +5,7 @@ require 'leaflet_helper/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "leaflet_helper"
8
- spec.version = '0.0.1'
8
+ spec.version = '0.0.2'
9
9
  spec.authors = ["Dewayne VanHoozer"]
10
10
  spec.email = ["dvanhoozer@gmail.com"]
11
11
 
@@ -2,25 +2,55 @@
2
2
 
3
3
  module LeafletHelper
4
4
  class L
5
- VERSION = '0.7.3'
6
- @@maps = []
5
+ VERSION = '0.7.3' # of leaflet.js
6
+ @@map_ids = [] # the div ids of all of the maps
7
+ @@markers = Hash.new([]) # An array of markers for each map id
7
8
  @@initialized = false
9
+
8
10
  class << self
9
11
 
10
- # intended for the layout.haml file in the head section
11
- def init
12
- return '' if @@initialized
13
- @@initialized = true
14
- return <<~EOS
12
+ # intended for the the head section
13
+ def init(options={})
14
+ o = {
15
+ markers: false
16
+ }.merge(options)
17
+
18
+ raise RuntimeError, 'Already initialized' if initialized?
19
+ initialized!
20
+
21
+ leaflet_script = <<~EOS
15
22
  <script type="text/javascript" src="//cdn.leafletjs.com/leaflet-#{VERSION}/leaflet.js"></script>
16
23
  <link rel="stylesheet" href="//cdn.leafletjs.com/leaflet-#{VERSION}/leaflet.css">
24
+
25
+ <script>
26
+
27
+ // set up AJAX request
28
+ ajaxRequest = getXmlHttpObject();
29
+
30
+ if (ajaxRequest == null) {
31
+ alert ("This browser does not support HTTP Request");
32
+ return;
33
+ }
34
+
35
+ // SMELL: this function is not map specific
36
+ function getXmlHttpObject() {
37
+ if (window.XMLHttpRequest) { return new XMLHttpRequest(); }
38
+ if (window.ActiveXObject) { return new ActiveXObject("Microsoft.XMLHTTP"); }
39
+ return null;
40
+ }
41
+
42
+ </script>
43
+
17
44
  EOS
45
+
46
+ return leaflet_script
18
47
  end # def init
19
48
 
49
+
20
50
  # intended for the body of a web page
21
51
  # hope to support multiple maps on a page
22
- def map(id='map', options={})
23
- @@maps << id
52
+ def place_map_here(id='map', options={})
53
+ @@map_ids << id
24
54
  o = {
25
55
  style: "width: 800px; height: 400px"
26
56
  }.merge(options)
@@ -29,42 +59,191 @@ module LeafletHelper
29
59
  div_options += " #{k.to_s}=" + '"' + v + '"'
30
60
  end
31
61
  return <<~EOS
32
- <div id="#{id}" #{div_options}></div>
62
+ <div id="#{id}"#{div_options}></div>
33
63
  EOS
34
64
  end # def map(id='map')
35
65
 
66
+
36
67
  # Intended for the body at the bottom
37
- def show_with_openstreetmap_layer(id="map", options={})
68
+ def show_map(id="map", options={})
38
69
  o = {
39
70
  latitude: 37.235,
40
71
  longitude: -115.811111,
41
72
  zoom: 9,
42
73
  min_zoom: 2,
43
- max_zoom: 22
74
+ max_zoom: 22
44
75
  }.merge(options)
45
- var_name = "lh_#{id}"
46
- return <<~EOS
76
+
77
+ var_name = get_var_name(id)
78
+
79
+ leaflet_script = <<~EOS
47
80
  <script>
48
81
 
49
82
  // set up the map
50
83
  #{var_name} = new L.Map('#{id}');
51
84
 
85
+ // start the map at a given location and zoom level
86
+ #{var_name}.setView(new L.LatLng(#{o[:latitude]}, #{o[:longitude]}), #{o[:zoom]});
87
+
88
+ </script>
89
+ EOS
90
+
91
+ return leaflet_script
92
+ end # def show_map(id="map", options={})
93
+
94
+
95
+ # Intended for the body at the bottom
96
+ def add_openstreetmap_layer(id="map", options={})
97
+ o = {
98
+ latitude: 37.235,
99
+ longitude: -115.811111,
100
+ zoom: 9,
101
+ min_zoom: 2,
102
+ max_zoom: 22
103
+ }.merge(options)
104
+
105
+ var_name = get_var_name(id)
106
+
107
+ leaflet_script = <<~EOS
108
+ <script>
109
+
52
110
  // create the tile layer with correct attribution
53
111
  var osmUrl ='http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
54
112
  var osmAttrib ='Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors';
55
113
  var osm = new L.TileLayer(osmUrl, {minZoom: #{o[:min_zoom]}, maxZoom: #{o[:max_zoom]}, attribution: osmAttrib});
56
-
57
- // start the map in South-East England
58
- #{var_name}.setView(new L.LatLng(#{o[:latitude]}, #{o[:longitude]}), #{o[:zoom]});
59
114
 
60
115
  #{var_name}.addLayer(osm);
61
116
 
62
117
  </script>
63
118
  EOS
64
- end # def show_with_openstreetmap_layer(id="map", options={})
119
+
120
+ return leaflet_script
121
+ end # def add_openstreetmap_layer(id="map", options={})
122
+
123
+
124
+ def add_support_for_markers(id="map", options={})
125
+ o = {
126
+ markers: true
127
+ }.merge(options)
128
+
129
+ var_name = get_var_name(id)
130
+
131
+ leaflet_script = <<~EOS
132
+ <script>
133
+
134
+ getMarkersFor#{id}(); // askForPlots();
135
+ #{var_name}.on('moveend', on#{id}Move);
136
+
137
+ function on#{id}Move(e) { getMarkersFor#{id}(); }
138
+
139
+
140
+
141
+ // request the marker info with AJAX for the current bounds
142
+ function getMarkersFor#{id}() { // was: askForPlots
143
+
144
+ var bounds = #{var_name}.getBounds();
145
+ var minll = bounds.getSouthWest();
146
+ var maxll = bounds.getNorthEast();
147
+
148
+ // FIXME: msg is the URL from which to get markers
149
+ var msg = 'leaflet/findbybbox.cgi?format=leaflet&bbox='+minll.lng+','+minll.lat+','+maxll.lng+','+maxll.lat;
150
+
151
+ ajaxRequest.onreadystatechange = stateChangedFor#{id};
152
+ ajaxRequest.open('GET', msg, true);
153
+ ajaxRequest.send(null);
154
+ }
155
+
156
+
157
+ function stateChangedFor#{id}() { // was: stateChanged
158
+
159
+ // if AJAX returned a list of markers, add them to the map
160
+ if (ajaxRequest.readyState == 4) {
161
+
162
+ // use the info here that was returned
163
+ if (ajaxRequest.status == 200) {
164
+ plotlist = eval("(" + ajaxRequest.responseText + ")");
165
+ removeMarkersFrom#{id}();
166
+
167
+ for (i=0; i<plotlist.length; i++) {
168
+ var plotll = new L.LatLng(plotlist[i].lat, plotlist[i].lon, true);
169
+ var plotmark = new L.Marker(plotll);
170
+ plotmark.data = plotlist[i];
171
+
172
+ #{var_name}.addLayer(plotmark);
173
+ plotmark.bindPopup("<h3>" + plotlist[i].name + "</h3>" + plotlist[i].details);
174
+ plotlayers.push(plotmark);
175
+ } // for
176
+
177
+ } // if (ajaxRequest.status == 200)
178
+
179
+ } // if (ajaxRequest.readyState == 4)
180
+
181
+ } // function stateChangedFor#{id}()
182
+
183
+
184
+ function removeMarkersFrom#{id}() { // was: removeMarkers
185
+ for (i=0; i<plotlayers.length; i++) {
186
+ #{var_name}.removeLayer(plotlayers[i]);
187
+ }
188
+ plotlayers = [];
189
+ }
190
+
191
+ </script>
192
+ EOS
193
+
194
+ return leaflet_script
195
+ end # def add_support_for_markers(map_id, map_options)
196
+
197
+
198
+ # TODO: extrach marker methods into a Marker class
199
+ def add_marker(marker={}, map_id='map')
200
+ # TODO: add_marker
201
+ end
202
+
203
+
204
+ def delete_marker(marker={}, map_id='map')
205
+ # TODO: delete_marker
206
+ end
207
+
208
+
209
+ def get_var_name(id="map")
210
+ return "lh_#{id}"
211
+ end
212
+
213
+
214
+ def comment(a_string='####################################################')
215
+ return "<!-- #{a_string} -->"
216
+ end
217
+
218
+
219
+ private
220
+
221
+ def initialized!
222
+ @@initialized = true
223
+ end
224
+
225
+
226
+ def initialized?
227
+ @@initialized
228
+ end
65
229
 
66
230
 
67
231
 
68
232
  end # class << self
69
233
  end # class L
70
234
  end # module LeafletHelper
235
+
236
+
237
+ __END__
238
+
239
+ A JSON marker array with one entry looks like this:
240
+
241
+ [
242
+ {
243
+ "name":"Tunbridge Wells, Langton Road, Burnt Cottage",
244
+ "lon":"0.213102",
245
+ "lat":"51.1429",
246
+ "details":"A Grade II listed five bedroom wing in need of renovation."
247
+ }
248
+ ]
249
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: leaflet_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dewayne VanHoozer
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-06-07 00:00:00.000000000 Z
11
+ date: 2016-06-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler