leaflet_helper 0.0.4 → 0.0.5
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 +4 -4
- data/README.md +251 -3
- data/leaflet_helper.gemspec +1 -1
- data/lib/leaflet_helper.rb +149 -55
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8c6c2bfb061ed25ac3ffdf96fef9e873cac10ff3
|
4
|
+
data.tar.gz: d23001011f855ba51e1b97ae012ea53e2cc57c25
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eb415d972d817fb45f1238c9364dc573b6673516b81982719bb01e6815645b2221c11dc8eb4240b966be9dc9b7e943a744b490b058db3d30ac062245488c28dd
|
7
|
+
data.tar.gz: d90dcb14eb057a44ebe7b6c6ccf276f4f13d48cf44e881e694c0c4c52c94563b8e0f42fbc102de094a0406f092969b2e0f70c5532bb7e51f89626a5714bf9476
|
data/README.md
CHANGED
@@ -7,7 +7,7 @@ Look at my experiments/maps repo to see how leafelet_helpers is used with a sina
|
|
7
7
|
|
8
8
|
## Installation
|
9
9
|
|
10
|
-
Add this line to your application's Gemfile
|
10
|
+
Add this line to your application's `Gemfile`:
|
11
11
|
|
12
12
|
```ruby
|
13
13
|
gem 'leaflet_helper'
|
@@ -23,12 +23,260 @@ Or install it yourself as:
|
|
23
23
|
|
24
24
|
## Usage
|
25
25
|
|
26
|
-
Look at https://github.com/MadBomber/experiments/tree/master/maps
|
26
|
+
Look at [https://github.com/MadBomber/experiments/tree/master/maps]
|
27
27
|
|
28
28
|
The views/layout.haml and views/index.haml show how to use the leaflet_helper. Of course you
|
29
29
|
have to require the gem in the main app.rb file in order for it to be available in the views. This
|
30
30
|
sinatra app also shows how to use markers on top of the main map layers.
|
31
31
|
|
32
|
+
Just about all of the methods in this library emit javascript source code.
|
33
|
+
|
34
|
+
### LeafletHelper::L.init(options={})
|
35
|
+
|
36
|
+
The 'init' method is used to insert the leaflet.js script and leaflet.css stylesheet.
|
37
|
+
It is used in the 'head' part of a webpage. For example if you are use Sinatra and haml then
|
38
|
+
the 'layout.haml' file might look like this:
|
39
|
+
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
%html
|
43
|
+
%head
|
44
|
+
= LeafletHelper::L.init
|
45
|
+
```
|
46
|
+
|
47
|
+
#### options
|
48
|
+
|
49
|
+
The 'options' is a hash. It is unused.
|
50
|
+
|
51
|
+
|
52
|
+
### LeafletHelper::L.place_map_here(id='map', options={})
|
53
|
+
|
54
|
+
The 'place_map_here' method is used to insert the HTML "<div>" tag into the
|
55
|
+
body of a webpage. This is the HTML container which will host the map image
|
56
|
+
managed by the LeafletJS library. If is possible to have several map images
|
57
|
+
displayed on the same webpage; HOWEVER, each map image MUST have a different
|
58
|
+
id ... because they MUST be unique ... because they MUST BE DIFFERENT thats why.
|
59
|
+
|
60
|
+
#### id
|
61
|
+
|
62
|
+
Yep, its a string that uniquely identifies the HTML `<div>` component
|
63
|
+
that holds the generated map graphic managed by the LeafletJS javascript
|
64
|
+
library.
|
65
|
+
|
66
|
+
#### options
|
67
|
+
|
68
|
+
Defaults:
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
o = {
|
72
|
+
style: "width: 800px; height: 400px"
|
73
|
+
}.merge(options)
|
74
|
+
```
|
75
|
+
|
76
|
+
Of course you could just use some CSS to add anything you want. But if you
|
77
|
+
are an inline kinda programmer then the options are for you.
|
78
|
+
|
79
|
+
### LeafletHelper::L.show_map(id="map", options={})
|
80
|
+
|
81
|
+
This method is used at the end of the body component of the HTML webpage. Why? you
|
82
|
+
ask. Because thats how we did it in the old days to make sure that the entire webpage
|
83
|
+
was loaded before doing stuff on it. You hotshot javascripters know how its done
|
84
|
+
the "right" way. I'm sure you have a pull-request already to go to help out us
|
85
|
+
old back-end geezers.
|
86
|
+
|
87
|
+
This method generates javascript source code that also includes the same javascript that
|
88
|
+
is generated by the LeafletHelper::L.set_view method. That may change.
|
89
|
+
|
90
|
+
#### id
|
91
|
+
|
92
|
+
Yep, its a string that uniquely identifies the HTML `<div>` component
|
93
|
+
that holds the generated map graphic managed by the LeafletJS javascript
|
94
|
+
library.
|
95
|
+
|
96
|
+
#### options
|
97
|
+
|
98
|
+
Defaults:
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
o = {
|
102
|
+
latitude: AREA51.latitude,
|
103
|
+
longitude: AREA51.longitude,
|
104
|
+
zoom: 9,
|
105
|
+
min_zoom: 2,
|
106
|
+
max_zoom: 22
|
107
|
+
}.merge(options)
|
108
|
+
```
|
109
|
+
|
110
|
+
These options are primarily for the 'setView' LeafletJS method. That method
|
111
|
+
establishes the location (latitude, longitude) at which the map is cenered. It
|
112
|
+
also establishs the current 'zoom' value and the minimum and maximum values for
|
113
|
+
which the zoom value can take.
|
114
|
+
|
115
|
+
|
116
|
+
### LeafletHelper::L.set_view(id='map', options={})
|
117
|
+
|
118
|
+
The javascript source code that this method generates moves an existing map
|
119
|
+
image to be centered at a specific latitude and longitude with a given zoom level.
|
120
|
+
|
121
|
+
#### id
|
122
|
+
|
123
|
+
Yep, its a string that uniquely identifies the HTML `<div>` component
|
124
|
+
that holds the generated map graphic managed by the LeafletJS javascript
|
125
|
+
library.
|
126
|
+
|
127
|
+
#### options
|
128
|
+
|
129
|
+
Defaults:
|
130
|
+
|
131
|
+
```ruby
|
132
|
+
o = {
|
133
|
+
latitude: AREA51.latitude,
|
134
|
+
longitude: AREA51.longitude,
|
135
|
+
zoom: 9
|
136
|
+
}.merge(options)
|
137
|
+
```
|
138
|
+
|
139
|
+
### LeafletHelper::L.add_openstreetmap_layer(id="map", options={})
|
140
|
+
|
141
|
+
The LeafletJS library by itself has no dataset - no roads, streets features etc. In
|
142
|
+
order to see that kind of stuff a "tile" layer - a zoomable set of images - needs to be
|
143
|
+
added. The most common and easiest to use free open source creative commons licensed
|
144
|
+
dataset is from the Open Street Map project.
|
145
|
+
|
146
|
+
This method adds the Open Street Map dataset tiles as a layer on the LeafletJS-managed
|
147
|
+
map image.
|
148
|
+
|
149
|
+
Other tile datasets are available such as those from the mapbox.com project. (See bolow)
|
150
|
+
|
151
|
+
#### id
|
152
|
+
|
153
|
+
Yep, its a string that uniquely identifies the HTML `<div>` component
|
154
|
+
that holds the generated map graphic managed by the LeafletJS javascript
|
155
|
+
library.
|
156
|
+
|
157
|
+
#### options
|
158
|
+
|
159
|
+
Defaults:
|
160
|
+
|
161
|
+
```ruby
|
162
|
+
o = {
|
163
|
+
latitude: AREA51.latitude,
|
164
|
+
longitude: AREA51.longitude,
|
165
|
+
zoom: 9,
|
166
|
+
min_zoom: 2,
|
167
|
+
max_zoom: 22
|
168
|
+
}.merge(options)
|
169
|
+
```
|
170
|
+
|
171
|
+
|
172
|
+
### LeafletHelper::L.add_mapbox_layer(id="map", options={})
|
173
|
+
|
174
|
+
As mentioned above in the discussion about Open Street Map, LeafletJS does not come
|
175
|
+
with image tiles. These must be provided from somewhere else. The Open Street Map has a
|
176
|
+
good set of vector-oriented images of streets and areas. It has no actual images
|
177
|
+
such as provided by satilites. These kinds of images can be obtained via the
|
178
|
+
Mapbox.com project.
|
179
|
+
|
180
|
+
In order to use Mapbox images you must have an account on mapbox.com. There is a nice
|
181
|
+
developer friendly zero dollar plan. Other plans for commerical use are reasoniblity priced.
|
182
|
+
|
183
|
+
With an account on mapbox.com you have access to their image tiles for use with the LeafletJS
|
184
|
+
library.
|
185
|
+
|
186
|
+
The following system environment variables can be used with this capability:
|
187
|
+
|
188
|
+
```
|
189
|
+
MAPBOX_URL
|
190
|
+
MAPBOX_USER
|
191
|
+
MAPBOX_STYLE_ID
|
192
|
+
MAPBOX_ACCESS_TOKEN
|
193
|
+
```
|
194
|
+
|
195
|
+
You can either provide the full URL for you mapbox project's images or you can
|
196
|
+
provide just the pieces that make up the URL - the user, style_id and access_token.
|
197
|
+
|
198
|
+
#### id
|
199
|
+
|
200
|
+
Yep, its a string that uniquely identifies the HTML `<div>` component
|
201
|
+
that holds the generated map graphic managed by the LeafletJS javascript
|
202
|
+
library.
|
203
|
+
|
204
|
+
#### options
|
205
|
+
|
206
|
+
Defaults:
|
207
|
+
|
208
|
+
```ruby
|
209
|
+
o = {
|
210
|
+
url: ENV['MAPBOX_URL'] || "https://api.mapbox.com/styles/v1/{mbUser}/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}",
|
211
|
+
mapbox_user: ENV['MAPBOX_USER'] || "your.mapbox.user.account",
|
212
|
+
style_id: ENV['MAPBOX_STYLE_ID'] || "your.mapbox.project.id",
|
213
|
+
access_token: ENV['MAPBOX_ACCESS_TOKEN'] || "your.mapbox.access.token",
|
214
|
+
attribution: 'Map data © <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>',
|
215
|
+
zoom: 9,
|
216
|
+
min_zoom: 2,
|
217
|
+
max_zoom: 22
|
218
|
+
}.merge(options)
|
219
|
+
```
|
220
|
+
|
221
|
+
|
222
|
+
### LeafletHelper::L.add_support_for_markers(id="map", options={})
|
223
|
+
|
224
|
+
While being able to show a map image on a webpage, zoom it and move it around (called panning),
|
225
|
+
it is much more fun if you can put actionable markers on top of the map. That is what this
|
226
|
+
method does. It injects the javascript code that makes the callback to get the "markers"
|
227
|
+
for display upon the map. The callback is generated everytime the map image changes either by
|
228
|
+
zoom or by pan.
|
229
|
+
|
230
|
+
The markers are provided as a JSON object. The object is an array of hashes. Each hash defines one marker.
|
231
|
+
|
232
|
+
|
233
|
+
|
234
|
+
#### id
|
235
|
+
|
236
|
+
Yep, its a string that uniquely identifies the HTML `<div>` component
|
237
|
+
that holds the generated map graphic managed by the LeafletJS javascript
|
238
|
+
library.
|
239
|
+
|
240
|
+
The only option that is necessary is the 'route' - the place to do an HTTP GET
|
241
|
+
to receive a JSON string of markers.
|
242
|
+
|
243
|
+
#### options
|
244
|
+
|
245
|
+
Defaults:
|
246
|
+
|
247
|
+
```ruby
|
248
|
+
o = {
|
249
|
+
route: "#{id}/markers"
|
250
|
+
}.merge(options)
|
251
|
+
```
|
252
|
+
|
253
|
+
Note that the 'route' default is setup to easily match a Sinatra block like:
|
254
|
+
|
255
|
+
```ruby
|
256
|
+
get '/:map_id/markers' do |map_id|
|
257
|
+
content-type :json
|
258
|
+
$array_of_marker_hashes[map_id].to_json
|
259
|
+
end
|
260
|
+
```
|
261
|
+
|
262
|
+
For now a marker hash looks like this:
|
263
|
+
|
264
|
+
```ruby
|
265
|
+
{
|
266
|
+
"name": "Area 51",
|
267
|
+
"lon": AREA51.longitude,
|
268
|
+
"lat": AREA51.latitude,
|
269
|
+
"details": "A good place to buy used flying saucers."
|
270
|
+
}
|
271
|
+
```
|
272
|
+
|
273
|
+
The marker when clicked shows a popup window on the map image. The "name" component is at
|
274
|
+
the top of the popup in an `<h3>` wrapper. The "lat" and "lon" elements allow LeafletJS to
|
275
|
+
place the marker at the correct location on the map image.
|
276
|
+
|
277
|
+
The "details" component can be any combination of HTML that you want. In one applicaton I have
|
278
|
+
that makes use of the LeafletHelper library I show thumbnails of images as part of the popup.
|
279
|
+
|
32
280
|
## Development
|
33
281
|
|
34
282
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -39,7 +287,7 @@ Of course if that doesn't work try something else. I'm not any good a javascrip
|
|
39
287
|
|
40
288
|
## Contributing
|
41
289
|
|
42
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
290
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/MadBomber/leaflet_helper.
|
43
291
|
|
44
292
|
|
45
293
|
## License
|
data/leaflet_helper.gemspec
CHANGED
data/lib/leaflet_helper.rb
CHANGED
@@ -1,12 +1,35 @@
|
|
1
1
|
# leaflet_helper.rb
|
2
|
+
#
|
3
|
+
# If you are a front-end developer and a Javascript guru this will
|
4
|
+
# make you laught. If you are a back-end big-data AI do-er of amazing
|
5
|
+
# things then this library might be of some use to you. I allows you
|
6
|
+
# to but maps onto a web-page using simple frameworks. No more NodeJS
|
7
|
+
# or Rails. Use some simple sinatra-based haml pages and voila you got maps.
|
8
|
+
#
|
9
|
+
# Makes use of these system environment variables when using
|
10
|
+
# using the method add_mapbox_layer:
|
11
|
+
#
|
12
|
+
# MAPBOX_URL
|
13
|
+
# MAPBOX_USER
|
14
|
+
# MAPBOX_STYLE_ID
|
15
|
+
# MAPBOX_ACCESS_TOKEN
|
16
|
+
|
17
|
+
# TODO: refactor
|
18
|
+
|
19
|
+
# Just as good a default as any other ...
|
20
|
+
AREA51 = Struct.new('Location', :latitude, :longitude).new
|
21
|
+
AREA51.latitude = 37.235
|
22
|
+
AREA51.longitude = -115.811111
|
2
23
|
|
3
24
|
module LeafletHelper
|
4
25
|
class L
|
5
|
-
VERSION
|
26
|
+
VERSION = '0.7.7' # of leaflet.js
|
27
|
+
JS = "http://cdn.leafletjs.com/leaflet/v#{VERSION}/leaflet.js"
|
28
|
+
CSS = "http://cdn.leafletjs.com/leaflet/v#{VERSION}/leaflet.css"
|
6
29
|
|
7
30
|
# TODO: Why are these desired?
|
8
|
-
@@map_ids
|
9
|
-
@@markers
|
31
|
+
@@map_ids = [] # the div ids of all of the maps
|
32
|
+
@@markers = Hash.new([]) # An array of markers for each map id
|
10
33
|
|
11
34
|
class << self
|
12
35
|
|
@@ -17,29 +40,9 @@ module LeafletHelper
|
|
17
40
|
}.merge(options)
|
18
41
|
|
19
42
|
leaflet_script = <<~EOS
|
20
|
-
|
21
|
-
<
|
22
|
-
|
23
|
-
// SMELL: I'm not sure this script section is needed.
|
24
|
-
<script>
|
25
|
-
|
26
|
-
// set up AJAX request
|
27
|
-
ajaxRequest = getXmlHttpObject();
|
28
|
-
|
29
|
-
if (ajaxRequest == null) {
|
30
|
-
alert ("This browser does not support HTTP Request");
|
31
|
-
// return;
|
32
|
-
}
|
33
|
-
|
34
|
-
// SMELL: this function is not map specific
|
35
|
-
function getXmlHttpObject() {
|
36
|
-
if (window.XMLHttpRequest) { return new XMLHttpRequest(); }
|
37
|
-
if (window.ActiveXObject) { return new ActiveXObject("Microsoft.XMLHTTP"); }
|
38
|
-
return null;
|
39
|
-
}
|
40
|
-
|
41
|
-
</script>
|
42
|
-
|
43
|
+
<!-- Leaflet -->
|
44
|
+
<script src="#{JS}"></script>
|
45
|
+
<link rel="stylesheet" href="#{CSS}">
|
43
46
|
EOS
|
44
47
|
|
45
48
|
return leaflet_script
|
@@ -50,15 +53,18 @@ module LeafletHelper
|
|
50
53
|
# can support multiple maps on a page; each
|
51
54
|
# map must have a unique id
|
52
55
|
def place_map_here(id='map', options={})
|
53
|
-
|
54
|
-
|
56
|
+
@@map_ids << id unless @@map_ids.include?(id)
|
57
|
+
|
55
58
|
o = {
|
56
59
|
style: "width: 800px; height: 400px"
|
57
60
|
}.merge(options)
|
61
|
+
|
58
62
|
div_options = ""
|
63
|
+
|
59
64
|
o.each_pair do |k,v|
|
60
65
|
div_options += " #{k.to_s}=" + '"' + v + '"'
|
61
66
|
end
|
67
|
+
|
62
68
|
return <<~EOS
|
63
69
|
<div id="#{id}"#{div_options}></div>
|
64
70
|
EOS
|
@@ -68,8 +74,8 @@ module LeafletHelper
|
|
68
74
|
# Intended for the body at the bottom
|
69
75
|
def show_map(id="map", options={})
|
70
76
|
o = {
|
71
|
-
latitude:
|
72
|
-
longitude:
|
77
|
+
latitude: AREA51.latitude,
|
78
|
+
longitude: AREA51.longitude,
|
73
79
|
zoom: 9,
|
74
80
|
min_zoom: 2,
|
75
81
|
max_zoom: 22
|
@@ -81,13 +87,17 @@ module LeafletHelper
|
|
81
87
|
<script>
|
82
88
|
|
83
89
|
var #{var_name};
|
84
|
-
|
90
|
+
|
91
|
+
// SMELL: These 3 vars are only used with markers so why are they here?
|
92
|
+
var ajaxRequestFor#{id};
|
85
93
|
var plotlistFor#{id};
|
86
94
|
var plotlayersFor#{id} = [];
|
87
95
|
|
88
96
|
// set up the map
|
89
97
|
#{var_name} = new L.Map('#{id}');
|
90
98
|
|
99
|
+
// SMELL: Maybe the app is not ready to set the view. There is
|
100
|
+
// a ruby method #set_view that does this.
|
91
101
|
// start the map at a given location and zoom level
|
92
102
|
#{var_name}.setView(new L.LatLng(#{o[:latitude]}, #{o[:longitude]}), #{o[:zoom]});
|
93
103
|
|
@@ -98,11 +108,35 @@ module LeafletHelper
|
|
98
108
|
end # def show_map(id="map", options={})
|
99
109
|
|
100
110
|
|
111
|
+
# Center an existing map to a specific location
|
112
|
+
def set_view(id='map', options={})
|
113
|
+
o = {
|
114
|
+
latitude: AREA51.latitude,
|
115
|
+
longitude: AREA51.longitude,
|
116
|
+
zoom: 9
|
117
|
+
}.merge(options)
|
118
|
+
|
119
|
+
var_name = get_var_name(id)
|
120
|
+
|
121
|
+
leaflet_script = <<~EOS
|
122
|
+
<script>
|
123
|
+
// center the map to a given location and zoom level
|
124
|
+
#{var_name}.setView(new L.LatLng(#{o[:latitude]}, #{o[:longitude]}), #{o[:zoom]});
|
125
|
+
</script>
|
126
|
+
EOS
|
127
|
+
|
128
|
+
return leaflet_script
|
129
|
+
|
130
|
+
end # def set_view(id='map', options={})
|
131
|
+
|
132
|
+
|
101
133
|
# Intended for the body at the bottom
|
134
|
+
# SMELL: why the bottom? Because that's they way we did it in the old days
|
135
|
+
# so that the entire web page would have been loaded by then.
|
102
136
|
def add_openstreetmap_layer(id="map", options={})
|
103
137
|
o = {
|
104
|
-
latitude:
|
105
|
-
longitude:
|
138
|
+
latitude: AREA51.latitude,
|
139
|
+
longitude: AREA51.longitude,
|
106
140
|
zoom: 9,
|
107
141
|
min_zoom: 2,
|
108
142
|
max_zoom: 22
|
@@ -118,7 +152,7 @@ module LeafletHelper
|
|
118
152
|
var osmAttrib ='Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors';
|
119
153
|
var osm = new L.TileLayer(osmUrl, {minZoom: #{o[:min_zoom]}, maxZoom: #{o[:max_zoom]}, attribution: osmAttrib});
|
120
154
|
|
121
|
-
#{var_name}
|
155
|
+
osm.addTo(#{var_name});
|
122
156
|
|
123
157
|
</script>
|
124
158
|
EOS
|
@@ -127,6 +161,42 @@ module LeafletHelper
|
|
127
161
|
end # def add_openstreetmap_layer(id="map", options={})
|
128
162
|
|
129
163
|
|
164
|
+
# Intended for the body at the bottom
|
165
|
+
# To use mapbox you must have an account
|
166
|
+
def add_mapbox_layer(id="map", options={})
|
167
|
+
o = {
|
168
|
+
url: ENV['MAPBOX_URL'] || "https://api.mapbox.com/styles/v1/{mbUser}/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}",
|
169
|
+
mapbox_user: ENV['MAPBOX_USER'] || "your.mapbox.user.account",
|
170
|
+
style_id: ENV['MAPBOX_STYLE_ID'] || "your.mapbox.project.id",
|
171
|
+
access_token: ENV['MAPBOX_ACCESS_TOKEN'] || "your.mapbox.access.token",
|
172
|
+
attribution: 'Map data © <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>',
|
173
|
+
zoom: 9,
|
174
|
+
min_zoom: 2,
|
175
|
+
max_zoom: 22
|
176
|
+
}.merge(options)
|
177
|
+
|
178
|
+
var_name = get_var_name(id)
|
179
|
+
|
180
|
+
leaflet_script = <<~EOS
|
181
|
+
<script>
|
182
|
+
|
183
|
+
// create the tile layer with correct attribution
|
184
|
+
L.tileLayer('#{o[:url]}', {
|
185
|
+
mbUser: '#{o[:mapbox_user]}',
|
186
|
+
attribution: '#{o[:attribution]}',
|
187
|
+
minZoom: #{o[:min_zoom]},
|
188
|
+
maxZoom: #{o[:max_zoom]},
|
189
|
+
id: '#{o[:style_id]}',
|
190
|
+
accessToken: '#{o[:access_token]}'
|
191
|
+
}).addTo(#{var_name});
|
192
|
+
|
193
|
+
</script>
|
194
|
+
EOS
|
195
|
+
|
196
|
+
return leaflet_script
|
197
|
+
end # def add_mapbox_layer(id="map", options={})
|
198
|
+
|
199
|
+
|
130
200
|
# Allows for the generation of markers on top of the map
|
131
201
|
def add_support_for_markers(id="map", options={})
|
132
202
|
o = {
|
@@ -138,14 +208,29 @@ module LeafletHelper
|
|
138
208
|
leaflet_script = <<~EOS
|
139
209
|
<script>
|
140
210
|
|
141
|
-
|
211
|
+
// set up AJAX request
|
212
|
+
ajaxRequestFor#{id} = getXmlHttpObjectFor#{id}();
|
213
|
+
|
214
|
+
if (ajaxRequestFor#{id} == null) {
|
215
|
+
alert ("This browser does not support HTTP Request - can not show markers");
|
216
|
+
// return;
|
217
|
+
}
|
218
|
+
|
219
|
+
function getXmlHttpObjectFor#{id}() {
|
220
|
+
if (window.XMLHttpRequest) { return new XMLHttpRequest(); }
|
221
|
+
if (window.ActiveXObject) { return new ActiveXObject("Microsoft.XMLHTTP"); }
|
222
|
+
return null;
|
223
|
+
}
|
224
|
+
|
225
|
+
|
226
|
+
getMarkersFor#{id}();
|
142
227
|
#{var_name}.on('moveend', on#{id}Move);
|
143
228
|
|
144
229
|
function on#{id}Move(e) { getMarkersFor#{id}(); }
|
145
230
|
|
146
231
|
|
147
232
|
// request the marker info for the current bounds
|
148
|
-
function getMarkersFor#{id}() {
|
233
|
+
function getMarkersFor#{id}() {
|
149
234
|
|
150
235
|
// TODO: Currently the displayed map bounds is not being used
|
151
236
|
// to get the new set of markers. This could be ugly for
|
@@ -157,9 +242,9 @@ module LeafletHelper
|
|
157
242
|
// this is the place from which the markers JSON array is obtained
|
158
243
|
var route = '#{o[:route]}';
|
159
244
|
|
160
|
-
|
161
|
-
|
162
|
-
|
245
|
+
ajaxRequestFor#{id}.onreadystatechange = stateChangedFor#{id};
|
246
|
+
ajaxRequestFor#{id}.open('GET', route, true);
|
247
|
+
ajaxRequestFor#{id}.send(null);
|
163
248
|
}
|
164
249
|
|
165
250
|
|
@@ -167,11 +252,11 @@ module LeafletHelper
|
|
167
252
|
function stateChangedFor#{id}() {
|
168
253
|
|
169
254
|
// if AJAX returned a list of markers, add them to the map
|
170
|
-
if (
|
255
|
+
if (ajaxRequestFor#{id}.readyState == 4) {
|
171
256
|
|
172
257
|
// use the info here that was returned
|
173
|
-
if (
|
174
|
-
plotlist = eval("(" +
|
258
|
+
if (ajaxRequestFor#{id}.status == 200) {
|
259
|
+
plotlist = eval("(" + ajaxRequestFor#{id}.responseText + ")");
|
175
260
|
removeMarkersFrom#{id}();
|
176
261
|
|
177
262
|
for (i=0; i<plotlist.length; i++) {
|
@@ -184,9 +269,9 @@ module LeafletHelper
|
|
184
269
|
plotlayersFor#{id}.push(plotmark);
|
185
270
|
} // for
|
186
271
|
|
187
|
-
} // if (
|
272
|
+
} // if (ajaxRequestFor#{id}.status == 200)
|
188
273
|
|
189
|
-
} // if (
|
274
|
+
} // if (ajaxRequestFor#{id}.readyState == 4)
|
190
275
|
|
191
276
|
} // function stateChangedFor#{id}()
|
192
277
|
|
@@ -206,6 +291,20 @@ module LeafletHelper
|
|
206
291
|
end # def add_support_for_markers(map_id, map_options)
|
207
292
|
|
208
293
|
|
294
|
+
# The "var name" is the Javascript container that has the map
|
295
|
+
def get_var_name(id="map")
|
296
|
+
return "lh_#{id}"
|
297
|
+
end
|
298
|
+
|
299
|
+
|
300
|
+
def comment(a_string='####################################################')
|
301
|
+
return "<!-- #{a_string} -->"
|
302
|
+
end
|
303
|
+
|
304
|
+
|
305
|
+
# TODO: Marker management is something that could be abstracted into
|
306
|
+
# its own class.
|
307
|
+
|
209
308
|
# Can't think of a way to use add_marker and delete_marker.
|
210
309
|
# They may not be necessary.
|
211
310
|
def add_marker(marker={}, map_id='map')
|
@@ -213,22 +312,17 @@ module LeafletHelper
|
|
213
312
|
end
|
214
313
|
|
215
314
|
|
315
|
+
# delete/remove a specific marker from a given map id
|
216
316
|
def delete_marker(marker={}, map_id='map')
|
217
317
|
# TODO: delete_marker
|
218
318
|
end
|
219
319
|
|
220
320
|
|
221
|
-
#
|
222
|
-
def
|
223
|
-
|
224
|
-
end
|
225
|
-
|
226
|
-
|
227
|
-
def comment(a_string='####################################################')
|
228
|
-
return "<!-- #{a_string} -->"
|
321
|
+
# clear/remove all markers for a given map id
|
322
|
+
def clear_markers(map_id='map')
|
323
|
+
@@markers[map_id] = []
|
229
324
|
end
|
230
325
|
|
231
|
-
|
232
326
|
end # class << self
|
233
327
|
end # class L
|
234
328
|
end # module LeafletHelper
|
@@ -241,8 +335,8 @@ A JSON marker array with one entry looks like this:
|
|
241
335
|
[
|
242
336
|
{
|
243
337
|
"name":"Tunbridge Wells, Langton Road, Burnt Cottage",
|
244
|
-
"lon":"0.213102",
|
245
|
-
"lat":"51.1429",
|
338
|
+
"lon":"0.213102", // SMELL: a string? really?
|
339
|
+
"lat":"51.1429", // SMELL: ditto
|
246
340
|
"details":"A Grade II listed five bedroom wing in need of renovation."
|
247
341
|
}
|
248
342
|
]
|
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.
|
4
|
+
version: 0.0.5
|
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-
|
11
|
+
date: 2016-06-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|