-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMapboxLayer.coffee
52 lines (38 loc) · 1.23 KB
/
MapboxLayer.coffee
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
mapboxgl = require 'mapbox-gl'
# Mapbox client connection status
connected = false
# Mapbox CSS string for insertCSS method
css = '@import "https://api.tiles.mapbox.com/mapbox-gl-js/v0.26.0/mapbox-gl.css"'
layerDefaults =
name: 'MapboxLayer'
mapDefaults =
navigation: false
attributionControl: false
zoom: 12.5
style: 'mapbox://styles/mapbox/streets-v9'
# Utility function to generate a valid and unique DOM ID
generateId = () ->
randLetter = String.fromCharCode(65 + Math.floor(Math.random() * 26))
id = randLetter + Date.now()
return id
# Setup accessToken for Mapbox
exports.connect = (token) ->
Utils.insertCSS(css)
mapboxgl.accessToken = token
connected = true
# Factory function to create a new MapLayer
exports.create = (mapOptions={}, layerOptions={}) ->
if !connected
throw new Error('MapLayer method ".connect(token)" is required.')
mapId = generateId()
layer = new Layer(_.defaults {
ignoreEvents: false
}, layerOptions, layerDefaults)
layer.classList.add(mapId)
map = new mapboxgl.Map(_.defaults {
container: document.querySelector('.' + mapId)
}, mapOptions, mapDefaults)
if mapOptions.navigation
map.addControl(new mapboxgl.Navigation())
layer.mapbox = map
return layer