-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
160 lines (134 loc) · 4.89 KB
/
script.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
/* set URL for GeoJSON file */
let geojsonURL = 'https://example.com/path/file.geojson';
/* if your domain sets cross-origin headers you can comment out the following*/
geojsonURL = 'https://cors-anywhere.herokuapp.com/' + geojsonURL;
/* choose property for selector */
const selectorProperty = 'county';
/* setup global variables for features */
let features, currentFeatures, index, popup;
/* disable event listener for our map move changes */
mapleft.un('moveend', onMoveEndleft);
/* add event listeners */
document.addEventListener( 'change', event => {
if (!event.target.matches('#geojson-selector')) return;
index = 0;
currentFeatures = features.filter( feature => event.target.value == feature.properties.county );
moveToFeature( index );
});
document.addEventListener( 'click', event => {
if( !event.target.matches('.geojson-button') ) return;
switch( event.target.dataset.direction ){
case 'next':
index = (index + 1) % currentFeatures.length;
break;
case 'prev':
index = (index == 0) && currentFeatures.length - 1 || index - 1;
break;
}
moveToFeature( index );
});
function moveToFeature(index){
var {geometry: {coordinates: [long, lat] }, properties } = currentFeatures[index];
let data = document.getElementById('geojson-properties');
displayProperties( data, properties );
mapleft.getView().setCenter(ol.proj.transform([long, lat], 'EPSG:4326', 'EPSG:3857'));
}
function displayProperties( element, properties ){
let markup = '';
Object.entries( properties ).forEach( ([key, value]) => {
if( 'geometry' !== key ) markup += '<dt>' + key + '</dt><dd>' + value + '</dd>';
});
element.innerHTML = markup;
}
/* add a select interaction for handling clicks on features */
const select = new ol.interaction.Select({});
select.on('select', function(event) {
popup.setPosition(event.mapBrowserEvent.coordinate);
let feature = event.selected[0];
let content = document.getElementById('popup-content');
displayProperties( content, feature.getProperties() );
});
/* create document fragment for inserting our panel */
let fragment = document.createDocumentFragment();
let wrapper = document.createElement('div');
wrapper.setAttribute('id', 'geojson-panel' );
let markup = '<select id="geojson-selector"><option>All counties</option></select>';
markup += '<button class="geojson-button" data-direction="prev">Previous feature</button>';
markup += '<button class="geojson-button" data-direction="next">Next feature</button>';
markup += '<dl id="geojson-properties"></dl>';
markup += '<div id="popup" class="ol-popup">';
markup += '<a href="#" id="geojson-popup-closer" class="ol-popup-closer"></a>';
markup += '<dl id="popup-content"></dl>';
markup += '</div>';
wrapper.innerHTML = markup;
fragment.appendChild( wrapper );
/* fetch geoJSON file and load */
fetch(geojsonURL)
.then((resp) => resp.json())
.then((data) => {
/* save data */
features = data.features;
currentFeatures = features;
index = 0;
/* create dropdown with sorted counties from data */
let allCounties = features.map( feature => feature.properties[selectorProperty] );
counties = [...new Set(allCounties)].sort();
let selector = fragment.getElementById('geojson-selector');
counties.map( (county) => {
let option = document.createElement('option');
option.setAttribute('value', county );
option.innerText = county;
selector.appendChild( option );
});
/* insert controls into header of page */
let header = document.getElementById( 'header' );
let body = document.getElementById('mapViewerSideBySide');
body.insertBefore( fragment, header );
/* create layer with markers for all features */
let vectorSource = new ol.source.Vector({
features: (new ol.format.GeoJSON()).readFeatures(data, {featureProjection: 'EPSG:3857'})
});
/* create the marker style for layer */
let iconStyle = new ol.style.Style({
image: new ol.style.Circle({
radius: 6,
fill: new ol.style.Fill({
color: 'green',
}),
stroke: new ol.style.Stroke({
color: 'white',
width: 1.5
})
})
});
/* add the source features to the layer, and apply a style to whole layer */
let vectorLayer = new ol.layer.Vector({
source: vectorSource,
style: iconStyle
});
/* add the layer to the left map (adding it to the right map affects the cursor)*/
mapleft.addLayer( vectorLayer );
/* add our popup handler */
mapleft.addInteraction(select);
/* create an overlay to anchor the popup to the map. */
const container = document.getElementById('popup');
popup = new ol.Overlay({
element: container,
autoPan: true,
autoPanAnimation: {
duration: 250
}
});
mapleft.addOverlay(popup);
/* add click handler for popup closer */
const closer = document.getElementById('geojson-popup-closer');
closer.onclick = function() {
popup.setPosition(undefined);
closer.blur();
return false;
};
/* set initial zoom for convenience */
mapleft.getView().setZoom(15);
/* message to indicate data are loaded */
console.log("GeoJSON: data loaded");
})