-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.html
210 lines (174 loc) · 5.88 KB
/
project.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
<!DOCTYPE html>
<html>
<head>
<title>Mapbox Marker Messages</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://api.mapbox.com/mapbox-gl-js/v2.7.0/mapbox-gl.css" rel="stylesheet" />
<style>
#map {
height: 500px;
}
</style>
</head>
<body>
<div id="map"></div>
<button id="downloadBtn">Download Markers as JSON</button>
<input type="file" id="uploadInput">
<script src="https://api.mapbox.com/mapbox-gl-js/v2.7.0/mapbox-gl.js"></script>
<script>
mapboxgl.accessToken = 'pk.eyJ1IjoiZGljZTEyMzQ1NiIsImEiOiJjbGs3djJ6YXEwYnk4M2VucGpuMDU0Ynd3In0.bg14TV_yK8WbM_7lO_k-3g';
// Initialize Map
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v11',
center: [0, 0], // Set initial map center
zoom: 1, // Set initial zoom level
});
var geolocate = new mapboxgl.GeolocateControl({
positionOptions: {
enableHighAccuracy: true
},
trackUserLocation: false
});
map.addControl(geolocate);
// Once the map is fully loaded, trigger the geolocation update
map.on('load', function () {
geolocate.trigger();
});
// Once the geolocation control initializes, listen for the 'geolocate' event
geolocate.on('geolocate', function (e) {
var lon = e.coords.longitude;
var lat = e.coords.latitude;
console.log('User location: ', lon, lat);
// Update the map's center and zoom to the user's location
map.flyTo({
center: [lon, lat],
zoom: 10 // Adjust the zoom level as desired
});
});
// Keep track of markers and their messages
var markers = [];
var messages = [];
// Add a marker to the map and bind a popup with a message
function addMarker(lngLat, message) {
var el = document.createElement('div');
el.className = 'marker';
el.style.backgroundImage = 'url(https://placekitten.com/g/30/30/)';
el.style.width = '30px';
el.style.height = '30px';
var popup = new mapboxgl.Popup({ offset: 25 }).setText(message);
var marker = new mapboxgl.Marker(el)
.setLngLat(lngLat)
.setPopup(popup)
.addTo(map);
markers.push(marker);
messages.push(message);
}
// Event listener for map click
map.on('click', function (e) {
var features = map.queryRenderedFeatures(e.point, { layers: ['custom-marker'] });
if (!features.length) {
var message = prompt('Enter your message.\nLeave blank to read someone else\'s message.');
if (message) {
addMarker(e.lngLat, message);
}
}
});
// Event listener for map zoom
map.on('zoom', function () {
markers.forEach(function (marker) {
var lngLat = marker.getLngLat();
marker.setLngLat(lngLat);
});
});
// Event listener for marker hover
map.on('mouseenter', 'custom-marker', function (e) {
map.getCanvas().style.cursor = 'pointer';
var coordinates = e.features[0].geometry.coordinates.slice();
var message = e.features[0].properties.message;
new mapboxgl.Popup()
.setLngLat(coordinates)
.setHTML('<p>' + message + '</p>')
.addTo(map);
});
// Event listener for marker leave
map.on('mouseleave', 'custom-marker', function () {
map.getCanvas().style.cursor = '';
map.getPopup().remove();
});
// Add a custom layer for the markers
map.on('load', function () {
map.addLayer({
id: 'custom-marker',
type: 'symbol',
source: {
type: 'geojson',
data: {
type: 'FeatureCollection',
features: [],
},
},
layout: {
'icon-image': 'cat', // Replace 'cat' with your custom icon image (you can upload an image to Mapbox and use its name here)
'icon-size': 0.6,
},
});
// Set the data for the custom layer
markers.forEach(function (marker, index) {
var feature = {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [markers[index].getLngLat().lng, markers[index].getLngLat().lat],
},
properties: {
message: messages[index],
},
};
map.getSource('custom-marker').setData({
type: 'FeatureCollection',
features: [...map.getSource('custom-marker')._data.features, feature],
});
});
});
// Function to download markers as JSON
function downloadMarkers() {
var data = markers.map(function (marker, index) {
return {
coordinates: [markers[index].getLngLat().lng, markers[index].getLngLat().lat],
message: messages[index],
};
});
var dataStr = JSON.stringify(data, null, 2);
var blob = new Blob([dataStr], { type: 'application/json' });
var url = URL.createObjectURL(blob);
var a = document.createElement('a');
a.style.display = 'none';
a.href = url;
a.download = 'markers.json';
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
}
// Event listener for the Download button
document.getElementById('downloadBtn').addEventListener('click', downloadMarkers);
function handleFileSelect(evt) {
var file = evt.target.files[0];
var reader = new FileReader();
reader.onload = function (e) {
var content = e.target.result;
var data = JSON.parse(content);
data.forEach(function (item) {
var message = item.message;
var lngLat = item.coordinates;
addMarker(lngLat, message);
});
};
reader.readAsText(file);
}
// Event listener for the file input change
document.getElementById('uploadInput').addEventListener('change', handleFileSelect);
</script>
</body>
</html>