-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.html
155 lines (135 loc) · 5.34 KB
/
map.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
<!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>
<style>
body {
margin: 0;
padding: 0;
}
#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
</style>
</head>
<body>
<!-- Create a container for the map -->
<div id='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
});
</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 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 () {
map.addSource("stores", { type: "geojson", data: macGeoJson });
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);
});
}
const geoJsonArray = { features: []};
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();
});
});
</script>
</body>
</html>