-
Notifications
You must be signed in to change notification settings - Fork 0
/
Map.js
146 lines (118 loc) · 4.25 KB
/
Map.js
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
class Map {
constructor(rootId, cartoClientCredentials, layers) {
// Initialize constants, classes, and map elements
this._COUNTY_LEVEL_ZOOM = 8
this._dataFetcher = new DataFetcher(cartoClientCredentials)
this._popupGenerator = new PopupGenerator()
this._cartoClient = new carto.Client(cartoClientCredentials);
this._map = L.map(rootId).setView([38.63765, -100.55221], 5);
// this._map.scrollWheelZoom.disable();
L.tileLayer(
'https://{s}.basemaps.cartocdn.com/rastertiles/dark_all/{z}/{x}/{y}.png',
{ maxZoom: 18 },
).addTo(this._map);
this._popup = L.popup({ closeButton: true });
// State
this._clickedLayersByLayerName = {
// [cartoLayerName]: {
// cartodb_id: null,
// boundary: null,
// }
// ...
}
// Layers:
// Specifically add the County Prep Layer to the map with a popup)
layers.forEach((layerInfo) => {
const cartoLayer = this._addMapLayerFromLayerInfo(layerInfo)
this._addPopupFor(cartoLayer, layerInfo, this._popupGenerator.genericPopupHTML)
})
this._addLayersToMap()
}
_addLayersToMap = () => this._cartoClient.getLeafletLayer().addTo(this._map);
_addMapLayerFromLayerInfo = (layerInfo) => {
const source = new carto.source.SQL(layerInfo.sql)
const style = new carto.style.CartoCSS(layerInfo.style)
const options = {
featureClickColumns: layerInfo.popupFieldInfo.map(({field}) => field)
}
const layer = new carto.layer.Layer(source, style, options)
this._cartoClient.addLayer(layer);
return layer
}
_removeAnyExistingClickedFeatureBorder = () => {
Object.values(this._clickedLayersByLayerName).forEach(clickedLayer => this._map.removeLayer(clickedLayer.boundary))
}
_addPopupFor = (layer, layerInfo, popupGeneratorFunction) => {
this._popup.on('remove', this._removeAnyExistingClickedFeatureBorder);
layer.off('featureOver');
layer.off('featureOut');
layer.on('featureClicked', featureEvent => this._updateOrToggleSelectedRegion(featureEvent, layerInfo, popupGeneratorFunction));
}
_updateOrToggleSelectedRegion = (featureEvent, layerInfo, popupGeneratorFunction) => {
const { existingPopupIsTogglingOff } = this._toggleClickedFeatureBorder(featureEvent, layerInfo.cartoLayerName, this._map) || {}
if (existingPopupIsTogglingOff) {
this.closePopup()
return
}
const popupInjectedWithData = popupGeneratorFunction(layerInfo, featureEvent)
this._popup.setContent(popupInjectedWithData);
this._popup.setLatLng(featureEvent.latLng);
if (!this._popup.isOpen()) {
this._popup.openOn(this._map);
}
}
_clearClickedLayers = () => this._clickedLayer = {}
_toggleClickedFeatureBorder = (featureEvent, cartoTableName) => {
this._removeAnyExistingClickedFeatureBorder()
const clickedPolygonCartoDBId = featureEvent.data.cartodb_id;
const layerIsAlreadySelected = Object.values(this._clickedLayersByLayerName).some(
clickedLayer => clickedLayer.cartodb_id === clickedPolygonCartoDBId
)
if (layerIsAlreadySelected) {
this._clearClickedLayers()
return { existingPopupIsTogglingOff: true }
}
this._dataFetcher.fetchFeatureOutline(cartoTableName, clickedPolygonCartoDBId)
.then(data => {
const geom = data[0].geom;
const boundary = L.geoJson(JSON.parse(geom), {
style: {
color: "#000",
weight: 5
}
});
this._map.addLayer(boundary);
this._clickedLayersByLayerName[cartoTableName] = {}
this._clickedLayersByLayerName[cartoTableName].boundary = boundary
this._clickedLayersByLayerName[cartoTableName].cartodb_id = clickedPolygonCartoDBId
})
}
_flyToCounty = (latLon, zoom = null) => {
const TRANSITION_DURATION = 3
this._map.flyTo(
latLon,
typeof(zoom) === 'number' || this._COUNTY_LEVEL_ZOOM,
{ duration: TRANSITION_DURATION }
)
}
// Public methods:
closePopup = () => {
this._map.closePopup();
this._clearClickedLayers();
}
// TODO: make this generic so we can highlight regions from outside the class
// selectCountyInCountyPrepLayer = (countyData) => {
// const latLng = [countyData.lat, countyData.lon]
//
// this._updateOrToggleSelectedRegion(
// {
// data: countyData,
// latLng,
// },
// this._countyPrepLayerInfo,
// this._popupGenerator.generateCountyPrepPopupHTML,
// )
//
// this._flyToCounty(latLng)
// }
}