Reload icons, add again source and layer on 'setStyle'

This commit is contained in:
Wolfgang Huß 2023-01-24 17:25:30 +01:00
parent 52d833c5b1
commit a9024b05be

View File

@ -71,9 +71,25 @@ export default {
users: null, users: null,
groups: null, groups: null,
markers: { markers: {
icons: [
{
id: 'marker-blue',
name: 'mapbox-marker-icon-20px-blue.png',
},
{
id: 'marker-orange',
name: 'mapbox-marker-icon-20px-orange.png',
},
{
id: 'marker-green',
name: 'mapbox-marker-icon-20px-green.png',
},
],
isImagesLoaded: false,
geoJSON: [], geoJSON: [],
isAdded: false, isGeoJSON: false,
isImagesAdded: false, isSourceAndLayerAdded: false,
isFlyToCenter: false,
}, },
} }
}, },
@ -101,6 +117,8 @@ export default {
}, },
streets: { streets: {
url: 'mapbox://styles/mapbox/streets-v11?optimize=true', url: 'mapbox://styles/mapbox/streets-v11?optimize=true',
// Wolle: url: 'mapbox://styles/mapbox/streets-v12',
// use the newest version?
}, },
satellite: { satellite: {
url: 'mapbox://styles/mapbox/satellite-streets-v11?optimize=true', url: 'mapbox://styles/mapbox/satellite-streets-v11?optimize=true',
@ -121,6 +139,7 @@ export default {
center: this.mapCenter, center: this.mapCenter,
zoom: this.mapZoom, zoom: this.mapZoom,
maxZoom: 22, maxZoom: 22,
// projection: 'globe', // the package is probably to old, because of Vue2: https://docs.mapbox.com/mapbox-gl-js/example/globe/
} }
}, },
mapCenter() { mapCenter() {
@ -134,6 +153,13 @@ export default {
onMapLoad({ map }) { onMapLoad({ map }) {
this.map = map this.map = map
map.on('style.load', (value) => {
// Triggered when `setStyle` is called.
this.markers.isImagesLoaded = false
this.markers.isSourceAndLayerAdded = false
this.loadMarkesIconsAndAddMarkers()
})
// // documentation of correct version: https://github.com/mapbox/mapbox-gl-language/tree/v0.10.0 // // documentation of correct version: https://github.com/mapbox/mapbox-gl-language/tree/v0.10.0
// // Add RTL support if you want to support Arabic // // Add RTL support if you want to support Arabic
// // Wolle: does not work yet // // Wolle: does not work yet
@ -151,6 +177,9 @@ export default {
// // is unclear, how to // // is unclear, how to
// // this.language.setLanguage('de') // makes error // // this.language.setLanguage('de') // makes error
// set the default atmosphere style
// this.map.setFog({}) // the package is probably to old, because of Vue2: https://docs.mapbox.com/mapbox-gl-js/example/globe/
// add search field for locations // add search field for locations
this.map.addControl( this.map.addControl(
new MapboxGeocoder({ new MapboxGeocoder({
@ -158,47 +187,37 @@ export default {
mapboxgl: this.mapboxgl, mapboxgl: this.mapboxgl,
}), }),
) )
// load markers // load markers
const markers = [ this.loadMarkesIconsAndAddMarkers()
{
id: 'marker-blue',
name: 'mapbox-marker-icon-20px-blue.png',
},
{
id: 'marker-red',
name: 'mapbox-marker-icon-20px-red.png',
},
{
id: 'marker-green',
name: 'mapbox-marker-icon-20px-green.png',
},
]
Promise.all(
markers.map(
(marker) =>
new Promise((resolve, reject) => {
// our images have to be in the 'static/img/*' folder otherwise they are not reachable via URL
map.loadImage('img/mapbox/marker-icons/' + marker.name, (error, image) => {
if (error) throw error
map.addImage(marker.id, image)
resolve()
})
}),
),
).then(() => {
this.markers.isImagesAdded = true
this.addMarkersOnCheckPrepared()
})
}, },
setStyle(url) { setStyle(url) {
this.map.setStyle(url) this.map.setStyle(url)
this.activeStyle = url this.activeStyle = url
}, },
loadMarkesIconsAndAddMarkers() {
Promise.all(
this.markers.icons.map(
(marker) =>
new Promise((resolve, reject) => {
// our images have to be in the 'static/img/*' folder otherwise they are not reachable via URL
this.map.loadImage('img/mapbox/marker-icons/' + marker.name, (error, image) => {
if (error) throw error
this.map.addImage(marker.id, image)
resolve()
})
}),
),
).then(() => {
this.markers.isImagesLoaded = true
this.addMarkersOnCheckPrepared()
})
},
addMarkersOnCheckPrepared() { addMarkersOnCheckPrepared() {
// set geoJSON for markers
if ( if (
!this.markers.isAdded && !this.markers.isGeoJSON &&
this.map && this.markers.isImagesLoaded &&
this.markers.isImagesAdded &&
this.currentUser && this.currentUser &&
this.users && this.users &&
this.groups this.groups
@ -209,7 +228,8 @@ export default {
type: 'Feature', type: 'Feature',
properties: { properties: {
type: 'the-user', type: 'the-user',
iconName: 'marker-blue', iconName: 'marker-orange',
iconRotate: 45.0,
title: this.currentUser.name, title: this.currentUser.name,
}, },
geometry: { geometry: {
@ -225,7 +245,8 @@ export default {
type: 'Feature', type: 'Feature',
properties: { properties: {
type: 'user', type: 'user',
iconName: 'marker-red', iconName: 'marker-blue',
iconRotate: 0.0,
title: user.name, title: user.name,
}, },
geometry: { geometry: {
@ -243,6 +264,7 @@ export default {
properties: { properties: {
type: 'group', type: 'group',
iconName: 'marker-green', iconName: 'marker-green',
iconRotate: 0.0,
title: group.name, title: group.name,
}, },
geometry: { geometry: {
@ -253,7 +275,11 @@ export default {
} }
}) })
this.markers.isGeoJSON = true
}
// add source and layer // add source and layer
if (!this.markers.isSourceAndLayerAdded && this.markers.isGeoJSON && this.map) {
this.map.addSource('markers', { this.map.addSource('markers', {
type: 'geojson', type: 'geojson',
data: { data: {
@ -266,20 +292,25 @@ export default {
type: 'symbol', type: 'symbol',
source: 'markers', source: 'markers',
layout: { layout: {
// get the "icon-image" from the source's "iconName" property 'icon-image': ['get', 'iconName'], // get the "icon-image" from the source's "iconName" property
'icon-image': ['get', 'iconName'], 'icon-allow-overlap': true,
'icon-size': 1.0, 'icon-size': 1.0,
// get the "text-field" from the source's "title" property 'icon-rotate': ['get', 'iconRotate'], // get the "icon-rotate" from the source's "iconRotate" property
'text-field': ['get', 'title'], 'text-field': ['get', 'title'], // get the "text-field" from the source's "title" property
'text-font': ['Open Sans Semibold', 'Arial Unicode MS Bold'], 'text-font': ['Open Sans Semibold', 'Arial Unicode MS Bold'],
'text-offset': [0, 0], 'text-offset': [0, 0],
'text-anchor': 'top', 'text-anchor': 'top',
'text-allow-overlap': true,
}, },
}) })
this.markers.isAdded = true this.markers.isSourceAndLayerAdded = true
}
// fly to center of never done and markers added
if (!this.markers.isFlyToCenter && this.markers.isSourceAndLayerAdded) {
this.mapFlyToCenter() this.mapFlyToCenter()
this.markers.isFlyToCenter = true
} }
}, },
mapFlyToCenter() { mapFlyToCenter() {