-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
248 lines (218 loc) · 7.05 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Singapore Hotels</title>
<script src='mapbox-gl.js'></script>
<link href='mapbox-gl.css' rel='stylesheet' />
<link href='base.css' rel='stylesheet' />
<style>
#map {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
#locations {
position: absolute;
}
.legend {
background-color: #fff;
border-radius: 3px;
bottom: 30px;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.10);
font: 12px/20px 'Helvetica Neue', Arial, Helvetica, sans-serif;
padding: 10px;
position: absolute;
right: 10px;
z-index: 1;
}
.legend h4 {
margin: 0 0 10px;
}
.legend p {
margin-left: 30px;
position: absolute;
display: block;
top: 0;
}
.legend div {
position:relative;
}
.legend div span {
border-radius: 50%;
display: inline-block;
margin-right: 5px;
opacity:0.8;
background-color:#E74C3C;
}
.mapboxgl-popup {
max-width: 400px;
font: 12px/20px 'Helvetica Neue', Arial, Helvetica, sans-serif;
}
</style>
</head>
<body>
<div id='map'></div>
<!-- style the legend !-->
<!-- add html elements to the legend !-->
<!--<div id='legend' class='legend'>
<h4>Price (RMB)</h4>
</div>!-->
<script>
//Put your Mapbox Public Access token here
mapboxgl.accessToken = 'pk.eyJ1Ijoidml2YW5zeHUiLCJhIjoiY2ppeWI1a2hjMDhtNTN3cGo3eDNxMGtjMSJ9.FjXwyZRWC_SGx2L5U74ezA';
//Load a new map in the 'map' HTML div
var map = new mapboxgl.Map({
container: 'map',
style: 'style.json',
center: [103.8497472, 1.291610266],
zoom: 11
});
// Store an array of quantiles
var max = 2400;
var fifth = 2400 / 6;
var quantiles = [];
for (i=0; i<6; i++) {
var quantile = (i + 1) * fifth;
quantiles.push(quantile);
}
//Load the vector tile source from our Mapbox Pedestrian traffic example
map.on('load', function() {
//Add the circle layer to the map
map.addLayer({
id: 'Singapore_hotels',
type: 'circle',
source: {
type: 'geojson',
data: 'features1.geojson' //Your Mapbox tileset Map ID
},
//name of tilesets
// 'source-layer': 'hotel_modified-1z3i3t',
paint: {
'circle-color': '#1CACFE',
//Add data-driven styles for circle radius
'circle-radius': {
property: 'price',
type: 'exponential',
stops: [
[0, 2],
[2400, 16]
]
},
'circle-opacity': 0.8
}
});
map.addLayer({
id: 'Singapore_meals',
type: 'circle',
source: {
type: 'geojson',
data: 'meals.geojson' //Your Mapbox tileset Map ID
},
//name of tilesets
// 'source-layer': 'hotel_modified-1z3i3t',
paint: {
'circle-color': '#00AB15',
//Add data-driven styles for circle radius
'circle-radius': {
property: 'price',
type: 'exponential',
stops: [
[4, 2],
[200, 16]
]
},
'circle-opacity': 0.2
}
});
// By default, exponential property functions have a base of 1.
// Exponential functions with a base of 1 are linear.
// The relationship between the radius of any point and the number of pedestrians is:
// radius = rateOfChange * numberOfPedestrians + radiusAtZero
/*var minRadius = 2;
var maxRadius = 16;
var minPrice = 0;
var maxPrice = 2400;
// Find the rateOfChange using two points
var rateOfChange = (maxRadius - minRadius) / (maxPrice - minPrice);
// Use the rateOfChange to solve for for radiusAtZero
// radius = rateOfChange * numberOfPedestrians + radiusAtZero
var radiusAtZero = maxRadius - (rateOfChange * maxPrice);
var legend = document.getElementById('legend');
function circleSize(quantile) {
var radius = (rateOfChange * quantile) + radiusAtZero;
var diameter = radius * 2;
return diameter;
}
quantiles.forEach(function(quantile){
legend.insertAdjacentHTML('beforeend', '<div><span style="width:' + circleSize(quantile) + 'px;height:' + circleSize(quantile) + 'px;margin: 0 ' + [(20 - circleSize(quantile)) / 2 ] + 'px"></span><p>' + quantile + '</p></div>');
});*/
// Add zoom and rotation controls to the map.
map.addControl(new mapboxgl.NavigationControl({
'position': 'top-left'
}));
// When a click event occurs near a place, open a popup at the location of
// the feature, with description HTML from its properties.
map.on('click', function(e) {
var features = map.queryRenderedFeatures(e.point, {
layers: ['Singapore_hotels']
});
if (!features.length) {
return;
}
var feature = features[0];
// Populate the popup and set its coordinates
// based on the feature found.
var popup = new mapboxgl.Popup()
.setLngLat(feature.geometry.coordinates)
.setHTML('<div id="popup" class="popup" style="z-index: 10;"> <h5> Detail: </h5>' +
'<ul class="list-group">' +
'<li class="list-group-item"> Name: ' + feature.properties['English_name'] + " </li>" +
'<li class="list-group-item"> Price: ¥' + feature.properties['price'] + " </li>" +
'<li class="list-group-item"> Rating: ' + feature.properties['rating'] + " </li>" +
'</ul> </div>')
.addTo(map);
});
// Use the same approach as above to indicate that the symbols are clickable
// by changing the cursor style to 'pointer'.
map.on('mousemove', function(e) {
var features = map.queryRenderedFeatures(e.point, {
layers: ['Singapore_hotels']
});
map.getCanvas().style.cursor = (features.length) ? 'pointer' : '';
});
map.on('click', function(e) {
var features1 = map.queryRenderedFeatures(e.point, {
layers: ['Singapore_meals']
});
if (!features1.length) {
return;
}
var feature1 = features1[0];
// Populate the popup and set its coordinates
// based on the feature found.
var popup1 = new mapboxgl.Popup()
.setLngLat(feature1.geometry.coordinates)
.setHTML('<div id="popup" class="popup" style="z-index: 10;"> <h5> Detail: </h5>' +
'<ul class="list-group">' +
'<li class="list-group-item"> Name: ' + feature1.properties['name'] + " </li>" +
'<li class="list-group-item"> Price: $' + feature1.properties['price'] + " </li>" +
'<li class="list-group-item"> Meal: ' + feature1.properties['meal'] + " </li>" +
'<li class="list-group-item"> Review Num: ' + feature1.properties['review_number'] + " </li>"+
'</ul> </div>')
.addTo(map);
});
// Use the same approach as above to indicate that the symbols are clickable
// by changing the cursor style to 'pointer'.
map.on('mousemove', function(e) {
var features1 = map.queryRenderedFeatures(e.point, {
layers: ['Singapore_meals']
});
map.getCanvas().style.cursor = (features1.length) ? 'pointer' : '';
});
});
</script>
</body>
</html>