| 
									
										
										
										
											2021-08-14 16:11:57 -06:00
										 |  |  | /* | 
					
						
							|  |  |  |  * This Source Code Form is subject to the terms of the Mozilla Public | 
					
						
							|  |  |  |  * License, v. 2.0. If a copy of the MPL was not distributed with this | 
					
						
							|  |  |  |  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-27 01:38:24 -06:00
										 |  |  | class MapControl { | 
					
						
							| 
									
										
										
										
											2021-10-02 19:57:41 -06:00
										 |  |  |     constructor(maplibreElement, interactive) { | 
					
						
							| 
									
										
										
										
											2021-08-14 16:11:57 -06:00
										 |  |  |         this.mapObj = null; | 
					
						
							| 
									
										
										
										
											2021-10-02 19:57:41 -06:00
										 |  |  |         this.mapEl = maplibreElement; | 
					
						
							| 
									
										
										
										
											2021-08-27 01:38:24 -06:00
										 |  |  |         this.interactiveMap = interactive == true; | 
					
						
							| 
									
										
										
										
											2021-08-14 16:11:57 -06:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-02 19:57:41 -06:00
										 |  |  |     static supported() { | 
					
						
							|  |  |  |         return maplibregl.supported(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-14 16:11:57 -06:00
										 |  |  |     createMap() { | 
					
						
							| 
									
										
										
										
											2021-10-02 19:57:41 -06:00
										 |  |  |         if (maplibregl.supported()) { | 
					
						
							| 
									
										
										
										
											2021-08-14 16:11:57 -06:00
										 |  |  |             $(this.mapEl).css("display", ""); | 
					
						
							| 
									
										
										
										
											2021-08-27 01:38:24 -06:00
										 |  |  |             this.mapObj = maplibreMap(this.mapEl, this.interactiveMap); | 
					
						
							| 
									
										
										
										
											2021-08-14 16:11:57 -06:00
										 |  |  |         } else { | 
					
						
							|  |  |  |             console.log("maplibre-gl not supported, disabling map"); | 
					
						
							|  |  |  |             $(this.mapEl).css("display", "none"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-27 01:38:24 -06:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Clear all markers from the map, make a new marker, and fly to it. | 
					
						
							|  |  |  |      * @param {string} classname CSS class for the marker, for adding icon and stuff. Default will be invisible and 0x0px. | 
					
						
							|  |  |  |      * @param {number} latitude | 
					
						
							|  |  |  |      * @param {number} longitude | 
					
						
							|  |  |  |      * @param {boolean} accurate set true to zoom to street level (z13), false to zoom to general area (z10). | 
					
						
							|  |  |  |      * @returns {undefined} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     clearMarkersAndCenterMapOnNewMarker(classname, latitude, longitude, accurate) { | 
					
						
							| 
									
										
										
										
											2021-08-14 16:11:57 -06:00
										 |  |  |         this.mapObj.removeMarkers(); | 
					
						
							|  |  |  |         this.mapObj.addMarker(latitude, longitude, classname); | 
					
						
							|  |  |  |         this.mapObj.animateMapIn(latitude, longitude, (accurate ? 13 : 10)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-27 01:38:24 -06:00
										 |  |  |     loadMarkersFromGeoJson(geojson, iconname, name) { | 
					
						
							|  |  |  |         this.mapObj.addSource("markers-" + name, { | 
					
						
							|  |  |  |             'type': 'geojson', | 
					
						
							|  |  |  |             'data': geojson | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         this.mapObj.addLayer({ | 
					
						
							|  |  |  |             id: "marker-layer-" + name, | 
					
						
							|  |  |  |             type: "symbol", | 
					
						
							|  |  |  |             source: "markers-" + name, | 
					
						
							|  |  |  |             layout: { | 
					
						
							|  |  |  |                 "icon-image": iconname, | 
					
						
							|  |  |  |                 "icon-anchor": "bottom", | 
					
						
							|  |  |  |                 'icon-allow-overlap': true | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     loadIcon(url, name, callback) { | 
					
						
							|  |  |  |         var mapObj = this.mapObj; | 
					
						
							|  |  |  |         this.mapObj.loadImage( | 
					
						
							|  |  |  |                 url, | 
					
						
							|  |  |  |                 function (error, image) { | 
					
						
							|  |  |  |                     if (error) | 
					
						
							|  |  |  |                         throw error; | 
					
						
							|  |  |  |                     mapObj.addImage(name, image); | 
					
						
							|  |  |  |                     callback(); | 
					
						
							|  |  |  |                 }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-14 16:11:57 -06:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Destroy and re-create the map. | 
					
						
							|  |  |  |      * @returns {undefined} | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     reloadMap() { | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             if (this.mapObj != null && typeof map != 'undefined') { | 
					
						
							|  |  |  |                 this.mapObj.off(); | 
					
						
							|  |  |  |                 this.mapObj.remove(); | 
					
						
							|  |  |  |                 this.mapObj = null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (document.getElementById("") != null) { | 
					
						
							|  |  |  |                     this.createMap(); | 
					
						
							|  |  |  |                 } else { | 
					
						
							|  |  |  |                     console.log("Info", "Not re-creating map because element is not in DOM."); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 this.createMap(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } catch (ex) { | 
					
						
							|  |  |  |             // oh well ¯\(°_o)/¯
 | 
					
						
							|  |  |  |             console.log(ex); | 
					
						
							|  |  |  |             $(this.mapEl).css("display", "none"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     setMapLocation(latitude, longitude) { | 
					
						
							| 
									
										
										
										
											2021-08-27 01:38:24 -06:00
										 |  |  |         if (this.mapObj == null) { | 
					
						
							| 
									
										
										
										
											2021-08-14 16:11:57 -06:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-08-27 01:38:24 -06:00
										 |  |  |         this.mapObj.setMapLocation(latitude, longitude); | 
					
						
							| 
									
										
										
										
											2021-08-14 16:11:57 -06:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     animateMapIn(latitude, longitude, zoom, heading) { | 
					
						
							|  |  |  |         if (this.mapObj == null) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (typeof zoom == 'undefined') { | 
					
						
							|  |  |  |             zoom = 10; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (typeof heading == 'undefined') { | 
					
						
							|  |  |  |             heading = 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         this.mapObj.animateMapIn(latitude, longitude, zoom, heading); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |