-
Notifications
You must be signed in to change notification settings - Fork 9
/
leaflet-map.js
150 lines (128 loc) · 4.42 KB
/
leaflet-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
/* leaflet-map.js - Leaflet JS Map Functions (https://leafletjs.com)
*
* Using OpenStreetMap (https://www.openstreetmap.org) or Mapbox (https://mapbox.com)
*
* 2020/09/05 Stephen Houser, N1SH
*/
/*
* Which map tile source to use:
* 'openstreetmap' -- use https://openstreetmap.org (does not use `mapAccessToken`)
* 'mapbox' -- use https://mapbox.com (requires `mapAccessToken` from mapbox)
*/
var mapTiles = 'openstreetmap';
var mapAccessToken = ''; // <-- put your mapbox token here if you are using mapbox, otherwise ignore.
/* Required Map functions:
*
* map = createMap(mapDivName)
* marker = createMarker(latitude, longitude, popupText)
* removeMarker(marker)
* removeAllMarkers()
*/
/* Internal variables */
var _map = null;
// Currently markers and polygons are maintained separately.
// There's no reason at this point for doing this.
// Should they be in one array of mapObjects and one 'feature group'?
var _markers = [];
var _markerFeatureGroup = null;
var _polygons = [];
var _polygonFeatureGroup = null;
/* createMap - Initialize and create map in named section of DOM
*
* Set up any handlers needed by the map and perform any initialization
* of the map library.
*/
function createMap(mapDivName) {
_map = L.map(mapDivName).setView([43.686292, -70.549876], 3);
var mapAttribution = 'Map data © <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, ' +
'<a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>';
if (mapTiles == 'openstreetmap') {
L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: mapAttribution,
}).addTo(_map);
} else if (mapTiles == 'mapbox') {
L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}', {
attribution: mapAttribution,
id: 'mapbox/streets-v11',
accessToken: mapAccessToken
}).addTo(_map);
} else {
alert('Map configuration error: No mapTiles source set. Cannot load maps!');
}
_markerFeatureGroup = new L.featureGroup();
_markerFeatureGroup.addTo(_map);
_polygonFeatureGroup = new L.featureGroup();
_polygonFeatureGroup.addTo(_map);
return _map;
}
/* createPolygon - create a polygon on the map with the given borders.
*
* - The first parameter is a list of [[lat, lon], ...]
* - The second parameter are 'options' for the polygon as an object {}
* for example `{color: 'red'}`
*/
function createPolygon(points, options) {
var poly = L.polygon(points, options);
poly.addTo(_polygonFeatureGroup);
_polygons.push(poly);
return poly;
}
/* removePolygon - remove the specified polygon from the map */
function removePolygon(poly) {
if (poly !== null) {
var polyIndex = _polygons.indexOf(poly);
if (polyIndex > -1) {
_polygons.splice(polyIndex, 1);
}
poly.remove();
}
}
/* removeAllPolygons - remove all the polygons from the map */
function removeAllPolygons() {
while (_polygons.length > 0) {
var poly = _polygons.pop();
removePolygon(poly);
}
}
/* createMarker - create a marker on the map at the given position.
*
* Set up a marker on the map and corresponding pop-up for when the
* marker is selected.
*/
function createMarker(latitude, longitude, popupText, options={}) {
// To use a smaller marker, use something like this...
// var blueMarkerSmall = L.icon({
// iconUrl: 'icons/blu-blank.png',
// iconSize: [32, 32], // size of the icon
// iconAnchor: [16, 32], // point of the icon which will correspond to marker's location
// popupAnchor: [0, -16] // point from which the popup should open relative to the iconAnchor
// });
//var marker = L.marker([latitude, longitude], {icon: blueMarkerSmall});
var marker = L.marker([latitude, longitude], options);
marker.addTo(_markerFeatureGroup)
.bindPopup(popupText);
_markers.push(marker);
return marker;
}
/* removeMarker - remove the given marker from the map. */
function removeMarker(marker) {
if (marker !== null) {
var markerIndex = _markers.indexOf(marker);
if (markerIndex > -1) {
_markers.splice(markerIndex, 1);
}
marker.remove();
}
}
/* removeAllMarkers - remove all markers from the map */
function removeAllMarkers() {
while (_markers.length > 0) {
var marker = _markers.pop();
removeMarker(marker);
}
}
/* zoomToAllMarkers - zoom the map to show all the current markers */
function zoomToAllMarkers() {
_map.fitBounds(_markerFeatureGroup.getBounds(), { padding: L.point(40, 40) });
}