Implement user location on map

- Deactivate map translation because it doesn't work.
This commit is contained in:
Wolfgang Huß 2023-01-11 16:18:00 +01:00
parent 9790eb1f88
commit 6d8149a77c
2 changed files with 75 additions and 20 deletions

View File

@ -17,6 +17,8 @@ export const locationAndBadgesFragment = (lang) => gql`
fragment locationAndBadges on User { fragment locationAndBadges on User {
location { location {
name: name${lang} name: name${lang}
lng
lat
} }
badges { badges {
id id

View File

@ -39,7 +39,11 @@
<MglNavigationControl position="top-right" /> <MglNavigationControl position="top-right" />
<MglGeolocateControl position="top-right" /> <MglGeolocateControl position="top-right" />
<MglScaleControl /> <MglScaleControl />
<MglMarker :coordinates="[10.452764, 51.165707]" color="blue" /> <MglMarker
v-if="this.currentUserCoordinates"
:coordinates="this.currentUserCoordinates"
color="blue"
/>
</mgl-map> </mgl-map>
</client-only> </client-only>
</div> </div>
@ -47,8 +51,10 @@
<script> <script>
import mapboxgl from 'mapbox-gl' import mapboxgl from 'mapbox-gl'
import MapboxLanguage from '@mapbox/mapbox-gl-language' // import MapboxLanguage from '@mapbox/mapbox-gl-language'
import { objectValuesToArray } from '../utils/utils' import { objectValuesToArray } from '../utils/utils'
import { mapGetters } from 'vuex'
import UserQuery from '~/graphql/User'
export default { export default {
name: 'Map', name: 'Map',
@ -61,9 +67,22 @@ export default {
return { return {
mapboxgl, mapboxgl,
activeStyle: null, activeStyle: null,
defaultCenter: [10.452764, 51.165707], // center of Germany: https://www.gpskoordinaten.de/karte/land/DE
currentUserLocation: null,
currentUserCoordinates: null,
} }
}, },
async mounted() {
this.currentUserLocation = await this.getUserLocation(this.currentUser.id)
this.currentUserCoordinates = this.currentUserLocation
? [this.currentUserLocation.lng, this.currentUserLocation.lat]
: null
this.mapFlyToCenter()
},
computed: { computed: {
...mapGetters({
currentUser: 'auth/user',
}),
styles() { styles() {
return { return {
available: objectValuesToArray(this.availableStyles), available: objectValuesToArray(this.availableStyles),
@ -94,36 +113,70 @@ export default {
return { return {
accessToken: this.$env.MAPBOX_TOKEN, accessToken: this.$env.MAPBOX_TOKEN,
style: !this.activeStyle ? this.availableStyles.outdoors.url : this.activeStyle, style: !this.activeStyle ? this.availableStyles.outdoors.url : this.activeStyle,
center: [10.452764, 51.165707], // center of Germany: https://www.gpskoordinaten.de/karte/land/DE center: this.mapCenter,
zoom: 4, zoom: this.mapZoom,
maxZoom: 22, maxZoom: 22,
} }
}, },
mapCenter() {
return this.currentUserCoordinates ? this.currentUserCoordinates : this.defaultCenter
},
mapZoom() {
return this.currentUserCoordinates ? 10 : 4
},
}, },
methods: { methods: {
onMapLoad({ map }) { onMapLoad({ map }) {
this.map = map this.map = map
// 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
mapboxgl.accessToken = this.$env.MAPBOX_TOKEN // // mapboxgl.accessToken = this.$env.MAPBOX_TOKEN
mapboxgl.setRTLTextPlugin('https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-rtl-text/v0.1.0/mapbox-gl-rtl-text.js') // // mapboxgl.setRTLTextPlugin('https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-rtl-text/v0.1.0/mapbox-gl-rtl-text.js')
const language = new MapboxLanguage({ // const language = new MapboxLanguage({
defaultLanguage: 'en', // Wolle // defaultLanguage: 'en', // Wolle
// defaultLanguage: 'de', // Wolle // // defaultLanguage: 'de', // Wolle
// defaultLanguage: 'auto', // Wolle // // defaultLanguage: 'auto', // Wolle
}) // })
this.language = language // this.language = language
this.map.addControl(language) // this.map.addControl(language)
// console.log('this.map: ', this.map) // // console.log('this.map: ', this.map)
// console.log('this.language: ', this.language) // // console.log('this.language: ', this.language)
// is unclear, how to // // is unclear, how to
// this.language.setLanguage('de') // makes error // // this.language.setLanguage('de') // makes error
// documentation: https://docs.mapbox.com/mapbox-gl-js/example/center-on-feature/
this.mapFlyToCenter()
}, },
setStyle(url) { setStyle(url) {
this.map.setStyle(url) this.map.setStyle(url)
this.activeStyle = url this.activeStyle = url
}, },
mapFlyToCenter() {
if (this.map) {
this.map.flyTo({
center: this.mapCenter,
zoom: this.mapZoom,
})
}
},
async getUserLocation(id) {
try {
const {
data: { User: users },
} = await this.$apollo.query({
query: UserQuery(this.$i18n),
variables: {
id,
followedByCount: 0,
followingCount: 0,
},
})
return users && users[0] && users[0].location ? users[0].location : null
} catch (err) {
this.$toast.error(err.message)
return null
}
},
}, },
} }
</script> </script>