Structures.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <template>
  2. <LayoutContainer>
  3. <v-responsive :aspect-ratio="1" width="100%">
  4. <!-- /!\ We do not use the nuxt-leaflet library here,
  5. because in its current version it's way too slow to deal with hundreds of markers -->
  6. <div id="map" />
  7. </v-responsive>
  8. <div class="advice">
  9. {{ $t("click_on_land_to_go_there") }}
  10. </div>
  11. <v-row class="map-shortcuts">
  12. <v-col v-for="shortcut in shortcuts" :key="shortcut.src" cols="2">
  13. <div @click="setMapBounds(shortcut.bounds)">
  14. <nuxt-img
  15. :src="shortcut.src"
  16. :alt="shortcut.alt"
  17. />
  18. </div>
  19. </v-col>
  20. </v-row>
  21. </LayoutContainer>
  22. </template>
  23. <script lang="ts">
  24. import 'leaflet/dist/leaflet.css'
  25. import Vue from 'vue'
  26. let L: any
  27. if (process.client) { // only require leaflet on client-side
  28. L = require('leaflet')
  29. }
  30. declare module 'vue/types/vue' {
  31. interface Vue {
  32. structures: Array<Structure>,
  33. zoomRequired: boolean,
  34. map: any,
  35. defaultBounds: L.LatLngBounds,
  36. shortcuts: Array<{src: string, alt: string, bounds: number[][] }>
  37. }
  38. }
  39. export default Vue.extend({
  40. props: {
  41. structures: {
  42. type: Array as () => Array<Structure>,
  43. required: false,
  44. default: () => []
  45. }
  46. },
  47. data () {
  48. return {
  49. map: null,
  50. defaultBounds: new L.LatLngBounds([51.03, -5.78], [41.2, 9.70]),
  51. shortcuts: [
  52. { src: '/images/metropole.png', alt: 'Metropole', bounds: [[51.03, -5.78], [41.2, 9.70]] },
  53. { src: '/images/guadeloupe.png', alt: 'Guadeloupe', bounds: [[16.62, -62.03], [15.74, -60.97]] },
  54. { src: '/images/martinique.png', alt: 'Martinique', bounds: [[14.95, -61.43], [14.28, -60.60]] },
  55. { src: '/images/mayotte.png', alt: 'Mayotte', bounds: [[-12.51, 44.86], [-13.19, 45.45]] },
  56. { src: '/images/la_reunion.png', alt: 'La Réunion', bounds: [[-20.65, 54.92], [-21.65, 56.15]] },
  57. { src: '/images/guyane.png', alt: 'Guyane', bounds: [[6.24, -54.62], [1.87, -50.59]] }
  58. ],
  59. zoomRequired: false
  60. }
  61. },
  62. watch: {
  63. structures (oldVal: Array<Structure>, newVal: Array<Structure>): void {
  64. if (oldVal !== newVal) {
  65. this.populateMarkers()
  66. }
  67. if (this.zoomRequired) {
  68. this._fitBoundsToMarkers()
  69. this.zoomRequired = false
  70. }
  71. }
  72. },
  73. mounted () {
  74. const defaultCenter: L.LatLngTuple = [46.71, 1.94]
  75. const defaultZoom: number = 5.75
  76. const layerSource: string = 'http://{s}.tile.osm.org/{z}/{x}/{y}.png'
  77. const attribution: string = '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
  78. const options: L.MapOptions = { scrollWheelZoom: false, zoomSnap: 0.25 }
  79. this.map = L.map('map', options)
  80. this.map.setView(defaultCenter, defaultZoom)
  81. L.tileLayer(layerSource, { attribution }).addTo(this.map)
  82. this.populateMarkers()
  83. this.map.on('zoomend moveend', () => {
  84. this.$emit('boundsUpdated', this.map.getBounds())
  85. })
  86. },
  87. beforeDestroy (): void {
  88. if (this.map) {
  89. this.map.remove()
  90. }
  91. },
  92. methods: {
  93. populateMarkers (): void {
  94. // remove any existant marker layers
  95. this.map.eachLayer((layer: L.Layer) => {
  96. if (layer instanceof L.MarkerClusterGroup) {
  97. this.map.removeLayer(layer)
  98. }
  99. })
  100. // populate new layer
  101. const clusters = L.markerClusterGroup()
  102. for (const s of this.structures) {
  103. if (!s.latitude || !s.longitude) { continue }
  104. const marker = L.marker([s.latitude, s.longitude])
  105. marker.bindPopup(`<b>${s.name}</b><br/>${s.postalCode} ${s.addressCity}<br/><a href="${s.website}" target="_blank">${s.website}</a>`)
  106. clusters.addLayer(marker)
  107. }
  108. this.map.addLayer(clusters)
  109. },
  110. setMapBounds (bounds: L.LatLngBoundsExpression): void {
  111. this.map.fitBounds(bounds) // << without this, the new bounds may not be properly set when the current view overlaps the new bounds.
  112. },
  113. resetBounds (): void {
  114. this.setMapBounds(this.defaultBounds)
  115. },
  116. zoomOnResults (): void {
  117. this.zoomRequired = true
  118. },
  119. _fitBoundsToMarkers (): void {
  120. const coords: L.LatLngBoundsLiteral = this.structures.filter(
  121. (s: Structure) => {
  122. return (
  123. s.latitude && typeof s.latitude !== 'undefined' &&
  124. s.longitude && typeof s.latitude !== 'undefined'
  125. )
  126. }
  127. ).map(
  128. (s: Structure) => { return [s.latitude as number, s.longitude as number] as L.LatLngTuple }
  129. )
  130. if (!coords) {
  131. return
  132. }
  133. this.setMapBounds(coords)
  134. }
  135. }
  136. })
  137. </script>
  138. <style scoped>
  139. #map {
  140. height: 100%;
  141. width: 100%;
  142. }
  143. .advice {
  144. margin: 1rem 0;
  145. color: #262626;
  146. font-weight: 750;
  147. text-align: center;
  148. font-size: 0.9rem;
  149. width: 100%;
  150. }
  151. .map-shortcuts {
  152. padding: 12px 6px;
  153. }
  154. .map-shortcuts img {
  155. border: solid 1px #000;
  156. width: 75px;
  157. height: 75px;
  158. }
  159. </style>