-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.js
98 lines (92 loc) · 3.23 KB
/
map.js
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
mapboxgl.accessToken = 'pk.eyJ1IjoiaHVnb3YyIiwiYSI6ImNraDZzbWd2YjAxdDMyc2xsOXQwaXc1cHAifQ.6oPEMuK5K9J8jyIDHyGijA';
var map = new mapboxgl.Map({
container: 'mapContainer',
style: 'mapbox://styles/hugov2/ckh6u3b701lfd19l7dld9hyo6', // stylesheet location
center: [43.6378, 1.4209], // starting position [lng, lat]
zoom: 1 // starting zoom
});
var mapboxClient = mapboxSdk({ accessToken: mapboxgl.accessToken });
var geojson = {
type: 'FeatureCollection',
features: [
]
};
var currentMarkers = [];
// set all markers on the map
function setMarkers(){
geojson.features.forEach(function (marker) {
// create a DOM element for the marker
if(marker.properties.country){
mapboxClient.geocoding
.forwardGeocode({
query: marker.properties.city+', '+marker.properties.country,
autocomplete: false,
limit: 1
})
.send()
.then(function (response) {
if (
response &&
response.body &&
response.body.features &&
response.body.features.length
) {
if(marker.properties.city){
var popup = new mapboxgl.Popup({ offset: 25 }).setHTML(
'<span style="font-weight:700; font-size:14px">'+marker.properties.name+'</span>'+"<br>"+"From : "+marker.properties.city+", "+marker.properties.country
);
}
else{
var popup = new mapboxgl.Popup({ offset: 25 }).setHTML(
'<span style="font-weight:700; font-size:14px">'+marker.properties.name+'</span>'+"<br>"+"From : "+marker.properties.country
);
}
var feature = response.body.features[0];
// check if there is already another marker at the same position
// if so, add the current artist name to the popup's data.
for(i = 0; i<currentMarkers.length; i++){
if(feature.center[0]==currentMarkers[i].getLngLat().lng && feature.center[1]==currentMarkers[i].getLngLat().lat){
var markerPopup = currentMarkers[i].getPopup();
currentMarkers[i].remove();
currentMarkers.splice(i,1);
var text = markerPopup._content.innerHTML;
var splitText = text.split('</button>');
var popup = new mapboxgl.Popup({ offset: 25 }).setHTML(
'<span style="font-weight:700; font-size:14px">'+marker.properties.name+'</span>'+'<br>'+splitText[1]
);
}
}
var pin = new mapboxgl.Marker({color: '#F9AA33', scale: 0.4})
.setLngLat(feature.center)
.setPopup(popup)
.addTo(map);
}
currentMarkers.push(pin);
});
}
});
};
async function resetMarker() {
$("#status").val(0);
if (currentMarkers!==null) {
for (var i = currentMarkers.length - 1; i >= 0; i--) {
currentMarkers[i].remove();
}
currentMarkers = [];
geojson.features = [];
var data = JSON.parse($("#playlistObjectItem").val());
data.forEach(function(artist){
var marker = {
type: 'Feature',
geometry: {
type: 'Point',
},
properties: {
}
};
marker.properties = artist;
geojson.features.push(marker);
});
}
setMarkers();
};