-
Notifications
You must be signed in to change notification settings - Fork 0
/
map-sidebar-geocode.html
277 lines (241 loc) · 10.8 KB
/
map-sidebar-geocode.html
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>Get started with the Isochrone API</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<!-- Import Mapbox GL JS -->
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v1.10.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v1.10.0/mapbox-gl.css' rel='stylesheet' />
<!-- Import Assembly -->
<link href='https://api.mapbox.com/mapbox-assembly/v0.23.2/assembly.min.css' rel='stylesheet'>
<script src='https://api.mapbox.com/mapbox-assembly/v0.23.2/assembly.js'></script>
<!-- Import jQuery -->
<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js'></script>
<!-- Import style for map with sidebar-->
<link href="sidebar.css" rel="stylesheet" type="text/css" />
</head>
<body>
<!--Import geocoder styles and script-->
<script
src="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v4.5.1/mapbox-gl-geocoder.min.js"></script>
<link rel="stylesheet"
href="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v4.5.1/mapbox-gl-geocoder.css"
type="text/css" />
<!-- Promise polyfill script required to use Mapbox GL Geocoder in IE 11 -->
<script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.auto.min.js"></script>
<!--Create a sidebar for store listing-->
<div class='sidebar'>
<div class='heading'>
<h1>McDonald's locations</h1>
</div>
<div id='listings' class='listings'></div>
</div>
<!-- Create a container for the map -->
<div id='map' class="map"></div>
<script>
// Add your Mapbox access token
mapboxgl.accessToken = 'pk.eyJ1IjoiY2FwdG1vbW8iLCJhIjoiY2thMnBtZTBhMGNteTNucG53Y3g2Y3gyNyJ9.Z4_j43zWekpkn2lX9gmoxQ';
// Basic map instance (zoomed to Austin, TX)
const map = new mapboxgl.Map({
container: "map",
style: "mapbox://styles/mapbox/dark-v10?optimize=true",
center: [103.8163, 1.3426],
zoom: 13
});
const geocoder = new MapboxGeocoder({
accessToken: mapboxgl.accessToken,
countries: 'sg',
mapboxgl: mapboxgl
});
map.addControl(geocoder, "top-right");
</script>
<script>
// Set up an object to track app state and data
const isoAppData = {
params: {
urlBase: "https://api.mapbox.com/isochrone/v1/mapbox/",
profile: "walking/",
minutes: 10,
category: "cafe"
}
};
// Get a single isochrone for a given position and return the GeoJSON
const getIso = function (position) {
// Build the URL for the isochrone API
const isoUrl = isoAppData.params.urlBase + isoAppData.params.profile + position.join(",") + "?contours_minutes=" +
isoAppData.params.minutes + "&polygons=true&access_token=" + mapboxgl.accessToken;
// Return the GeoJSON
return fetch(isoUrl).then(res => res.json());
};
// Update the map sources so the isochrones draw on the map
const setIsos = function (isos, store) {
map.addSource(store, {
type: "geojson",
data: {
type: "FeatureCollection",
features: [
]
}
});
map.addLayer({
"id": "isoLayer" + store,
"type": "fill",
"source": store,
"layout": {},
"paint": {
"fill-color": "yellow",
"fill-opacity": 0.3
}
}, "poi-label");
// Update the map
map.getSource(store).setData(isos);
//geoJsonArray.features.push({ geo : isos, name: store.name })
};
// Get the isochrone data from the API, then update the map
const getSetIsos = function (store) {
// Once the isochrones are received, update the map
let point = [store.long, store.lat]
getIso(point).then(values => {
setIsos(values, store);
});
};
const addCronuts = function (stores) {
stores.forEach(element => {
console.log(element.geo, element.name);
setIsos(element.geo, element.name);
//getSetIsos(element);
});
}
const addListing = function buildLocationList(jsonData, geojsonData) {
jsonData.forEach(function (store, i) {
/**
* Create a shortcut for `store.properties`,
* which will be used several times below.
**/
var prop = store;
/* Add a new listing section to the sidebar. */
var listings = document.getElementById('listings');
var listing = listings.appendChild(document.createElement('div'));
/* Assign a unique `id` to the listing. */
listing.id = "listing-" + prop.id;
/* Assign the `item` class to each listing for styling. */
listing.className = 'item';
/* Add the link to the individual listing created above. */
var link = listing.appendChild(document.createElement('a'));
link.href = '#';
link.className = 'title';
link.id = "link-" + prop.id;
link.innerHTML = prop.address;
//add data
link.setAttribute("data-name", `${prop.name}`);
/* Add details to the individual listing. */
var details = listing.appendChild(document.createElement('div'));
details.innerHTML = prop.city;
if (prop.phone) {
details.innerHTML += ' · ' + `${prop.phone ? prop.phone : 'N/A'}`;
}
link.addEventListener('click', function (e) {
for (let i = 0; i < geojsonData.features.length; i++) {
if (this.dataset.name == geojsonData.features[i].properties.name) {
let clickedListing = geojsonData.features[i];
console.log(clickedListing);
flyToStore(clickedListing);
createPopUp(clickedListing);
}
}
var activeItem = document.getElementsByClassName('active');
if (activeItem[0]) {
activeItem[0].classList.remove('active');
}
this.parentNode.classList.add('active');
});
});
}
function flyToStore(currentFeature) {
map.flyTo({
center: currentFeature.geometry.coordinates,
zoom: 15
});
}
function createPopUp(currentFeature) {
var popUps = document.getElementsByClassName('mapboxgl-popup');
/** Check if there is already a popup on the map and if so, remove it */
if (popUps[0]) popUps[0].remove();
var popup = new mapboxgl.Popup({ closeOnClick: false })
.setLngLat(currentFeature.geometry.coordinates)
.setHTML(`<h3>${currentFeature.properties.name}</h3>` +
'<h4>' + currentFeature.properties.address + '</h4>')
.addTo(map);
}
const macJson = 'https://gist.githubusercontent.com/captmomo/b19136360712e691c0898c4b34842965/raw/19ca1a06af9b045f6e9af88019b9e53577e1b188/macdonalds.json';
const macGeoJson = 'https://gist.githubusercontent.com/captmomo/b19136360712e691c0898c4b34842965/raw/d5e420345edd4fea8555856ca1977be9e5765bf7/mcdonalds.geojson';
const macIso = 'https://gist.githubusercontent.com/captmomo/b19136360712e691c0898c4b34842965/raw/4fe415d3b7fd9a73b99b9c253be5535507769328/mcdisochrone.json';
const addMarkers = function (jsonData, parsedGeo) {
parsedGeo.features.forEach(function (store, i) {
let geoProp = store.properties;
for (let i = 0; i < parsedGeo.features.length; i++) {
if (geoProp.name == jsonData[i].name) {
geoProp.address = jsonData[i].address;
geoProp.id = jsonData[i].id;
//alert(clickedListing);
}
};
});
map.addSource("stores", { type: "geojson", data: parsedGeo });
map.addLayer({
id: "stores",
type: "circle",
source: "stores",
paint: {
"circle-color": "#FFC72C",
"circle-radius": 2,
"circle-stroke-color": "#DA291c",
"circle-stroke-opacity": 1,
"circle-stroke-width": 3
}
});
map.on("click", "stores", function (e) {
// new mapboxgl.Popup()
// .setLngLat(e.lngLat)
// .setHTML(e.features[0].properties.name)
// .addTo(map);
let clickedPoint = e.features[0];
flyToStore(clickedPoint);
createPopUp(clickedPoint);
let activeListing = document.getElementsByClassName('active');
if (activeListing[0]) {
activeListing.classList.remove('active');
}
console.log(clickedPoint.properties);
let listing = document.getElementById('listing-' + clickedPoint.properties.id);;
listing.classList.add('active');
});
}
const geoJsonArray = { features: [] };
const fetchJson = function getJson(url) {
return fetch(url)
.then(res => res.json());
};
const getListingData = function () {
let plain = fetchJson(macJson);
let geoInfo = fetchJson(macGeoJson);
Promise.all([plain, geoInfo]).then(values => {
addListing(values[0], values[1]);
addMarkers(values[0], values[1]);
});
};
map.on('load', function () {
// When the map loads, add the source and layer
fetch(macIso)
.then(res => res.json())
.then(result => {
addCronuts(result.features);
//addMarkers();
});
getListingData();
});
</script>
</body>
</html>