Structures.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. const zoomed = this._fitBoundsToMarkers()
  69. if (!zoomed) {
  70. this.setMapBounds(this.defaultBounds)
  71. }
  72. this.zoomRequired = false
  73. }
  74. }
  75. },
  76. mounted () {
  77. const defaultCenter: L.LatLngTuple = [46.71, 1.94]
  78. const defaultZoom: number = 5.75
  79. const layerSource: string = 'http://{s}.tile.osm.org/{z}/{x}/{y}.png'
  80. const attribution: string = '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
  81. const options: L.MapOptions = { scrollWheelZoom: false, zoomSnap: 0.25 }
  82. this.map = L.map('map', options)
  83. this.map.setView(defaultCenter, defaultZoom)
  84. L.tileLayer(layerSource, { attribution }).addTo(this.map)
  85. this.populateMarkers()
  86. this.map.on('zoomend moveend', () => {
  87. this.$emit('boundsUpdated', this.map.getBounds())
  88. })
  89. },
  90. beforeDestroy (): void {
  91. if (this.map) {
  92. this.map.remove()
  93. }
  94. },
  95. methods: {
  96. populateMarkers (): void {
  97. // remove any existant marker layers
  98. this.map.eachLayer((layer: L.Layer) => {
  99. if (layer instanceof L.MarkerClusterGroup) {
  100. this.map.removeLayer(layer)
  101. }
  102. })
  103. // populate new layer
  104. const clusters = L.markerClusterGroup()
  105. for (const s of this.structures) {
  106. if (!s.latitude || !s.longitude) { continue }
  107. const marker = L.marker([s.latitude, s.longitude])
  108. marker.bindPopup(`<b>${s.name}</b><br/>${s.postalCode} ${s.addressCity}<br/><a href="${s.website}" target="_blank">${s.website}</a>`)
  109. clusters.addLayer(marker)
  110. }
  111. this.map.addLayer(clusters)
  112. },
  113. setMapBounds (bounds: L.LatLngBoundsExpression): void {
  114. this.map.fitBounds(bounds) // << without this, the new bounds may not be properly set when the current view overlaps the new bounds.
  115. },
  116. resetBounds (): void {
  117. this.setMapBounds(this.defaultBounds)
  118. },
  119. zoomOnResults (): void {
  120. this.zoomRequired = true
  121. },
  122. _fitBoundsToMarkers (): boolean {
  123. const coords: L.LatLngBoundsLiteral = this.structures.filter(
  124. (s: Structure) => {
  125. return (
  126. s.latitude && typeof s.latitude !== 'undefined' &&
  127. s.longitude && typeof s.latitude !== 'undefined'
  128. )
  129. }
  130. ).map(
  131. (s: Structure) => { return [s.latitude as number, s.longitude as number] as L.LatLngTuple }
  132. )
  133. if (!(coords.length > 0)) {
  134. return false
  135. }
  136. this.setMapBounds(coords)
  137. return true
  138. }
  139. }
  140. })
  141. </script>
  142. <style scoped>
  143. #map {
  144. height: 100%;
  145. width: 100%;
  146. }
  147. .advice {
  148. margin: 1rem 0;
  149. color: #262626;
  150. font-weight: 750;
  151. text-align: center;
  152. font-size: 0.9rem;
  153. width: 100%;
  154. }
  155. .map-shortcuts {
  156. padding: 12px 6px;
  157. }
  158. .map-shortcuts img {
  159. border: solid 1px #000;
  160. width: 75px;
  161. height: 75px;
  162. }
  163. </style>