forked from CivicTechAtlanta/mapsforus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.js
189 lines (162 loc) · 6.46 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
window.onload = function () {
var documentSettings = {};
function createMarkerIcon(icon, prefix, markerColor, iconColor) {
return L.AwesomeMarkers.icon({
icon: icon,
prefix: prefix,
markerColor: markerColor,
iconColor: iconColor
});
}
function centerAndZoomMap(points) {
var mapCenter = L.latLng();
var mapZoom = 0;
// center and zoom map based on points or to user-specified zoom and center
if (documentSettings["Initial Center Latitude:"] !== '' && documentSettings["Initial Center Longitude:"] !== '') {
// center and zoom
mapCenter = L.latLng(documentSettings["Initial Center Latitude:"], documentSettings["Initial Center Longitude:"]);
map.setView(mapCenter);
} else {
var groupBounds = points.getBounds();
mapZoom = map.getBoundsZoom(groupBounds);
mapCenter = groupBounds.getCenter();
}
if (documentSettings["Initial Zoom:"] !== '') {
mapZoom = parseInt(documentSettings["Initial Zoom:"]);
}
map.setView(mapCenter, mapZoom);
// once map is recentered, open popup in center of map
if (documentSettings["Info Popup Text:"] !== '') {
initInfoPopup(documentSettings["Info Popup Text:"], mapCenter);
};
}
// possibly refactor this so you can add points to layers without knowing what all the layers are beforehand
// run this function after document is loaded but before mapPoints()
function determineLayers(points) {
var layerNamesFromSpreadsheet = [];
var layers = {};
for (var i in points) {
var pointLayerNameFromSpreadsheet = points[i].Layer;
if (pointLayerNameFromSpreadsheet !== "" && layerNamesFromSpreadsheet.indexOf(pointLayerNameFromSpreadsheet) === -1) {
layerNamesFromSpreadsheet.push(pointLayerNameFromSpreadsheet);
}
}
// if none of the points have named layers or if there was only one name, return no layers
if (layerNamesFromSpreadsheet.length === 1) {
layers = undefined;
} else {
for (var i in layerNamesFromSpreadsheet) {
var layerNameFromSpreadsheet = layerNamesFromSpreadsheet[i];
layers[layerNameFromSpreadsheet] = L.layerGroup();
layers[layerNameFromSpreadsheet].addTo(map);
}
}
return layers;
}
// only run this after Tabletop has loaded (onTabletopLoad())
function mapPoints(points, layers) {
var markerArray = [];
for (var i in points) {
var point = points[i];
if (point.Latitude !== "" && point.Longitude !== "") {
var marker = L.marker([point.Latitude, point.Longitude], {
icon: createMarkerIcon(point['Marker Icon'], 'fa', point['Marker Color'].toLowerCase(), point['Marker Icon Color'])
}).bindPopup("<b>" + point["Title"] + "</b><br>" + point["Description"]);
if (layers !== undefined && layers.length !== 1) {
marker.addTo(layers[point.Layer]);
}
markerArray.push(marker);
}
}
var group = L.featureGroup(markerArray);
// if layers.length === 0, add points to map instead of layer
if (layers === undefined || layers.length === 0) {
clusterMarkers(group);
} else {
L.control.layers(null, layers, {
collapsed: false
}).addTo(map);
}
centerAndZoomMap(group);
}
// only run this after Tabletop has loaded (onTabletopLoad())
function mapHeatmap(points) {
var markerArray = [];
var boundsArray = [];
for (var i in points) {
var point = points[i];
if (point.Latitude !== "" && point.Longitude !== "") {
markerArray.push([Number(point.Latitude), Number(point.Longitude), Number(point.Intensity)]);
// TODO: use layers
var marker = L.marker([point.Latitude, point.Longitude]); // TODO: this is only used for bounds, so it should be removed and bounds should be found another way
boundsArray.push(marker);
}
}
var heatmapMaxZoom = documentSettings["Heatmap Max Zoom:"] || 17;
var heatmap = L.heatLayer(markerArray, {
radius: 35,
blur: 35,
maxZoom: heatmapMaxZoom
}).addTo(map);
var group = L.featureGroup(boundsArray);
centerAndZoomMap(group);
}
// reformulate documentSettings as a dictionary, e.g.
// {"webpageTitle": "Leaflet Boilerplate", "infoPopupText": "Stuff"}
function createDocumentSettings(settings) {
documentSettings = {};
for (var i in settings) {
var setting = settings[i];
documentSettings[setting.Setting] = setting.Customization;
}
}
function clusterMarkers(group) {
// cluster markers, or don't
if (documentSettings["Markercluster:"] === 'on') {
var cluster = L.markerClusterGroup({
polygonOptions: {
opacity: 0.3,
weight: 3
}
});
cluster.addLayer(group);
map.addLayer(cluster);
} else {
map.addLayer(group);
}
}
function onTabletopLoad() {
createDocumentSettings(tabletop.sheets(constants.informationSheetName).elements);
addBaseMap();
document.title = documentSettings["Webpage Title:"];
var points = tabletop.sheets(constants.pointsSheetName).elements;
var layers = determineLayers(points);
if (documentSettings["Map Type:"] === 'Heatmap') {
mapHeatmap(points);
} else {
mapPoints(points, layers);
}
}
var tabletop = Tabletop.init( { key: constants.googleDocID, // from constants.js
callback: function(data, tabletop) { onTabletopLoad() }
});
function initInfoPopup(info, coordinates) {
L.popup({className: 'intro-popup'})
.setLatLng(coordinates) // this needs to change
.setContent(info)
.openOn(map);
}
function addBaseMap() {
var basemap = documentSettings["Tile Provider:"] === '' ? 'Stamen.TonerLite' : documentSettings["Tile Provider:"];
L.tileLayer.provider(basemap, {
maxZoom: 18
}).addTo(map);
L.control.attribution({
position: 'bottomleft'
}).addTo(map);
var attributionHTML = document.getElementsByClassName("leaflet-control-attribution")[0].innerHTML;
var mapCreatorAttribution = documentSettings["Your Name:"] === '' ? 'Built with' : 'This map was built by ' + documentSettings["Your Name:"] + ' using';
attributionHTML = mapCreatorAttribution + ' <a href="http://mapsfor.us/">mapsfor.us</a><br><a href="http://mapsfor.us/">Mapsfor.us</a> was created by <a href="http://www.codeforatlanta.org/">Code for Atlanta</a><br>' + attributionHTML;
document.getElementsByClassName("leaflet-control-attribution")[0].innerHTML = attributionHTML;
}
};