-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapbox code
More file actions
235 lines (185 loc) · 5.96 KB
/
Copy pathMapbox code
File metadata and controls
235 lines (185 loc) · 5.96 KB
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
<head>
<!-- Mapbox Resources -->
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v2.9.1/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v2.9.1/mapbox-gl.css' rel='stylesheet' />
<style>
/*style map popups*/
.mapboxgl-popup-content {
pointer-events: auto;
border-radius: 4px;
box-shadow: none;
padding: 12px 16px;
color: #161616;
background-color: #fefae0;
}
/*popup bottom arrow color*/
.mapboxgl-popup-anchor-bottom .mapboxgl-popup-tip {
border-top-color: #fefae0;
}
</style>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
<script>
$(".locations-map_wrapper").removeClass("is--show");
//-----------MAPBOX SETUP CODE BELOW-----------
mapboxgl.accessToken = "REPLACE ACCESS TOKEN WITH YOURS HERE"
// create empty locations geojson object
let mapLocations = {
type: "FeatureCollection",
features: [],
};
let selectedMapLocations = [];
// Initialize map and load in #map wrapper
let map = new mapboxgl.Map({
container: "map",
style: "mapbox://styles/kckatcreative/clcdprcpm000814n0clb6knwm",
center: [108.30527846628165, 14.31929816265803],
zoom: 6.59,
});
// Adjust zoom of map for mobile and desktop
let mq = window.matchMedia("(min-width: 480px)");
if (mq.matches) {
map.setZoom(6.59); //set map zoom level for desktop size
} else {
map.setZoom(6); //set map zoom level for mobile size
}
// Add zoom and rotation controls to the map.
map.addControl(new mapboxgl.NavigationControl());
// Get cms items
let listLocations = document.getElementById("location-list").childNodes;
// For each colleciton item, grab hidden fields and convert to geojson proerty
function getGeoData() {
listLocations.forEach(function (location, i) {
console.log(location);
let locationLat = location.querySelector("#locationLatitude").value;
let locationLong = location.querySelector("#locationLongitude").value;
let locationInfo = location.querySelector(".locations-map_card").innerHTML;
let coordinates = [locationLong, locationLat];
let locationID = location.querySelector("#locationID").value;
//add array ID
let arrayID = (i + 1) - 1;
let geoData = {
type: "Feature",
geometry: {
type: "Point",
coordinates: coordinates,
},
properties: {
id: locationID,
description: locationInfo,
arrayID: arrayID,
},
};
if (mapLocations.features.includes(geoData) === false) {
mapLocations.features.push(geoData);
}
});
console.log(mapLocations);
}
// Invoke function
getGeoData();
// define mapping function to be invoked later
function addMapPoints() {
/* Add the data to your map as a layer */
map.addLayer({
id: "locations",
type: "circle",
/* Add a GeoJSON source containing place coordinates and information. */
source: {
type: "geojson",
data: mapLocations,
},
paint: {
"circle-radius": 8,
"circle-stroke-width": 1,
"circle-color": "#FF9900",
"circle-opacity": 1,
"circle-stroke-color": "#405F3B",
},
});
// open a popup with the correct location
function addPopup(e) {
// Copy coordinates array.
const coordinates = e.features[0].geometry.coordinates.slice();
const description = e.features[0].properties.description;
// Ensure that if the map is zoomed out such that multiple
// copies of the feature are visible, the popup appears
// over the copy being pointed to.
while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) {
coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360;
}
new mapboxgl.Popup().setLngLat(coordinates).setHTML(description).addTo(map);
}
// When a click event occurs on a feature in the places layer, open a popup at the
// location of the feature, with description HTML from its properties.
map.on("click", "locations", (e) => {
//find ID of collection item in array
const ID = e.features[0].properties.arrayID;
//add popup
addPopup(e);
//show webflow Collection module
$(".locations-map_wrapper").addClass("is--show");
//Check if an item is currently there
if ($(".locations-map_item.is--show").length) {
$(".locations-map_item").removeClass("is--show");
}
//find collection item by array ID and show it
$(".locations-map_item").eq(ID).addClass("is--show");
});
// Center the map on the coordinates of any clicked circle from the 'locations' layer.
map.on("click", "locations", (e) => {
map.flyTo({
center: e.features[0].geometry.coordinates,
speed: 0.5,
curve: 1,
easing(t) {
return t;
},
});
});
// Change the cursor to a pointer when the mouse is over the 'locations' layer.
map.on("mouseenter", "locations", () => {
map.getCanvas().style.cursor = "pointer";
});
// Change it back to a pointer when it leaves.
map.on("mouseleave", "locations", () => {
map.getCanvas().style.cursor = "";
});
}
//When map is loaded initialize with data
map.on("load", function (e) {
addMapPoints();
});
//close side nav with button
$(".close-block").click(function(){
$(".locations-map_wrapper").removeClass("is--show");
});
//hover stuff
//set hover popup
const popup = new mapboxgl.Popup({
closeButton: false,
closeOnClick: false
});
map.on('mouseenter', 'locations', (e) => {
// Change the cursor style as a UI indicator.
map.getCanvas().style.cursor = 'pointer';
// Copy coordinates array.
const coordinates = e.features[0].geometry.coordinates.slice();
const description = e.features[0].properties.description;
// Ensure that if the map is zoomed out such that multiple
// copies of the feature are visible, the popup appears
// over the copy being pointed to.
while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) {
coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360;
}
// Populate the popup and set its coordinates
// based on the feature found.
popup.setLngLat(coordinates).setHTML(description).addTo(map);
});
map.on('mouseleave', 'locations', () => {
map.getCanvas().style.cursor = '';
popup.remove();
});
</script>
</body>