This repository has been archived by the owner on May 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
91 lines (86 loc) · 4.41 KB
/
index.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
import mapboxgl from 'mapbox-gl';
import 'mapbox-gl/dist/mapbox-gl.css';
import MapboxGeocoder from '@mapbox/mapbox-gl-geocoder';
import '@mapbox/mapbox-gl-geocoder/dist/mapbox-gl-geocoder.css';
import { MapboxStyleSwitcherControl } from "@watergis/mapbox-gl-style-switcher";
import "@watergis/mapbox-gl-style-switcher/styles.css"
import MapboxPopupControl from '@watergis/mapbox-gl-popup';
import '@watergis/mapbox-gl-popup/css/styles.css';
import MapboxPitchToggleControl from '@watergis/mapbox-gl-pitch-toggle-control';
import '@watergis/mapbox-gl-pitch-toggle-control/css/styles.css';
import MapboxAreaSwitcherControl from '@watergis/mapbox-gl-area-switcher';
import '@watergis/mapbox-gl-area-switcher/css/styles.css';
import { MapboxLegendControl } from "@watergis/mapbox-gl-legend";
import '@watergis/mapbox-gl-legend/css/styles.css';
import { MapboxExportControl } from "@watergis/mapbox-gl-export";
import '@watergis/mapbox-gl-export/css/styles.css';
import MapboxElevationControl from "@watergis/mapbox-gl-elevation";
import '@watergis/mapbox-gl-elevation/css/styles.css';
import { MapboxValhallaControl} from "@watergis/mapbox-gl-valhalla";
import '@watergis/mapbox-gl-valhalla/css/styles.css';
import axios from 'axios';
import config from './config';
(()=>{
mapboxgl.accessToken = config.accessToken;
const map = new mapboxgl.Map({
container: 'map',
style: config.styles[0].uri,
center: config.center,
zoom: config.zoom,
hash:true,
attributionControl: false,
});
map.addControl(new mapboxgl.NavigationControl(), 'top-right');
map.addControl(new mapboxgl.GeolocateControl({positionOptions: {enableHighAccuracy: true},trackUserLocation: true}), 'top-right');
map.addControl(new MapboxPitchToggleControl({minpitchzoom: 19}));
MapboxStyleSwitcherControl.DEFAULT_STYLE = config.styles[0].title;
map.addControl(new MapboxStyleSwitcherControl(config.styles), 'top-right');
map.addControl(new MapboxAreaSwitcherControl(config.areaSwitcher.areas), 'top-right');
map.addControl(new MapboxElevationControl(config.elevation.url, config.elevation.options), 'top-right');
map.addControl(new MapboxExportControl({Crosshair: true, PrintableArea: true}), 'top-right');
map.addControl(new MapboxValhallaControl(config.valhalla.url, config.valhalla.options),'top-right');
map.addControl(new mapboxgl.ScaleControl({maxWidth: 80, unit: 'metric'}), 'bottom-left');
map.addControl(new mapboxgl.AttributionControl({compact: true,customAttribution: config.attribution}), 'bottom-right');
if (config.popup) map.addControl(new MapboxPopupControl(config.popup.target));
if (config.legend){
const legendCtrl = new MapboxLegendControl(config.legend.targets, config.legend.options);
map.addControl(legendCtrl, 'bottom-left')
}
if (config.search){
axios.get(config.search.url)
.then(res=>{
const customerData = res.data;
function forwardGeocoder(query) {
var matchingFeatures = [];
for (var i = 0; i < customerData.features.length; i++) {
var feature = customerData.features[i];
config.search.target.forEach(v=>{
var target = feature.properties[v];
if (!target){
return;
}
// handle queries with different capitalization than the source data by calling toLowerCase()
if ((target.toString().toLowerCase().search(query.toString().toLowerCase()) !== -1)) {
feature['place_name'] = config.search.format(feature.properties);
feature['center'] = feature.geometry.coordinates;
feature['place_type'] = config.search.place_type;
matchingFeatures.push(feature);
}
})
}
return matchingFeatures;
}
map.addControl(
new MapboxGeocoder({
accessToken: mapboxgl.accessToken,
localGeocoder: forwardGeocoder,
localGeocoderOnly:true,
zoom: config.search.zoom,
placeholder: config.search.placeholder,
mapboxgl: mapboxgl,
}),
'top-left'
);
});
}
})();