Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 104 additions & 7 deletions assets/js/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,111 @@
// Set language
map.addControl(new MapboxLanguage());

// Add markers
$.each(container.data('markers'), function(index, data) {
bounds.extend([data.longitude, data.latitude]);
map.on('load', () => {
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please describe the solution, e.g. source (is this an official one? especially randomizing coordinates).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the code from the documentation you provided in the task description..
Markers with the same address creating on the same place so it unavailable to click them separately so I've added random offset to their spawn position

map.addSource('markers', {
type: 'geojson',
data: {
"type": "FeatureCollection",
"features": container.data('markers').map(data => ({
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
data.longitude + (Math.random() - 0.5) * 0.0005,
data.latitude + (Math.random() - 0.5) * 0.0005,
],
},
"properties": {
"content": data.content,
},
})),
},
cluster: true,
clusterMaxZoom: 14,
clusterRadius: 50,
});

var marker = new mapboxgl.Marker()
.setLngLat([data.longitude, data.latitude])
.setPopup(new mapboxgl.Popup().setHTML(data.content))
.addTo(map);
map.loadImage(hivepressGeolocationData.assetURL + '/images/markerclustererplus/m1.png', (error, image) => {
if (error) {
throw error;
}

map.addImage('custom-cluster', image);

map.addLayer({
id: 'clusters',
type: 'symbol',
source: 'markers',
filter: ['has', 'point_count'],
layout: {
'icon-image': 'custom-cluster',
'text-field': ['get', 'point_count_abbreviated'],
'text-size': 14,
'text-anchor': 'center',
},
paint: {
'text-color': '#000',
},
});
});

map.addLayer({
id: 'unclustered-point',
type: 'circle',
source: 'markers',
filter: ['!', ['has', 'point_count']],
paint: {
'circle-color': '#11b4da',
'circle-radius': 5,
'circle-stroke-width': 2,
'circle-stroke-color': '#fff',
},
});

map.on('click', 'unclustered-point', (e) => {
var coordinates = e.features[0].geometry.coordinates.slice(),
content = e.features[0].properties.content;

if (['mercator', 'equirectangular'].includes(map.getProjection().name)) {
while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) {
coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360;
}
}

new mapboxgl.Popup()
.setLngLat(coordinates)
.setHTML(content)
.addTo(map);
});

map.on('click', 'clusters', (e) => {
var features = map.queryRenderedFeatures(e.point, {
layers: ['clusters']
}),
clusterId = features[0].properties.cluster_id;

map.getSource('markers').getClusterExpansionZoom(
clusterId,
(err, zoom) => {
if (err) {
return;
}

map.easeTo({
center: features[0].geometry.coordinates,
zoom: zoom
});
}
);
});

map.on('mouseenter', 'clusters', () => {
map.getCanvas().style.cursor = 'pointer';
});

map.on('mouseleave', 'clusters', () => {
map.getCanvas().style.cursor = '';
});
});

// Fit bounds
Expand Down
2 changes: 1 addition & 1 deletion assets/js/common.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.