Structures.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 {
  26. GridLayer,
  27. LatLngBounds,
  28. LatLngBoundsExpression, LatLngBoundsLiteral,
  29. LatLngTuple,
  30. MapOptions
  31. } from 'leaflet'
  32. import Vue from 'vue'
  33. let L: any
  34. if (typeof window !== 'undefined') {
  35. // only require leaflet on client-side
  36. L = require('leaflet')
  37. }
  38. export default Vue.extend({
  39. props: {
  40. structures: {
  41. type: Array as () => Array<Structure>,
  42. required: false,
  43. default: () => []
  44. }
  45. },
  46. data () {
  47. const options: MapOptions = { scrollWheelZoom: false, zoomSnap: 0.25 }
  48. return {
  49. map: L.map('map', options),
  50. defaultBounds: new 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>) {
  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: Array<number> = [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. this.map.setView(defaultCenter, defaultZoom)
  79. L.tileLayer(layerSource, { attribution }).addTo(this.map)
  80. this.populateMarkers()
  81. this.map.on('zoomend moveend', () => {
  82. this.$emit('boundsUpdated', this.map.getBounds())
  83. })
  84. },
  85. beforeDestroy () {
  86. if (this.map) {
  87. this.map.remove()
  88. }
  89. },
  90. methods: {
  91. populateMarkers () {
  92. // remove any existant marker layers
  93. this.map.eachLayer((layer: GridLayer) => {
  94. if (layer instanceof L.MarkerClusterGroup) {
  95. this.map.removeLayer(layer)
  96. }
  97. })
  98. // populate new layer
  99. const clusters = L.markerClusterGroup()
  100. for (const s of this.structures) {
  101. if (!s.latitude || !s.longitude) { continue }
  102. const marker = L.marker([s.latitude, s.longitude])
  103. marker.bindPopup(`<b>${s.name}</b><br/>${s.postalCode} ${s.addressCity}<br/><a href="${s.website}" target="_blank">${s.website}</a>`)
  104. clusters.addLayer(marker)
  105. }
  106. this.map.addLayer(clusters)
  107. },
  108. setMapBounds (bounds: LatLngBoundsExpression) {
  109. this.map.fitBounds(bounds) // << without this, the new bounds may not be properly set when the current view overlaps the new bounds.
  110. },
  111. resetBounds () {
  112. this.setMapBounds(this.defaultBounds)
  113. },
  114. zoomOnResults () {
  115. this.zoomRequired = true
  116. },
  117. _fitBoundsToMarkers () {
  118. const coords: LatLngBoundsLiteral = this.structures.filter(
  119. (s) => {
  120. return (
  121. s.latitude && typeof s.latitude !== 'undefined' &&
  122. s.longitude && typeof s.latitude !== 'undefined'
  123. )
  124. }
  125. ).map(
  126. (s) => { return [s.latitude as number, s.longitude as number] as LatLngTuple }
  127. )
  128. if (!coords) {
  129. return
  130. }
  131. this.setMapBounds(coords)
  132. }
  133. }
  134. })
  135. </script>
  136. <style scoped>
  137. #map {
  138. height: 100%;
  139. width: 100%;
  140. }
  141. .advice {
  142. margin: 1rem 0;
  143. color: #262626;
  144. font-weight: 750;
  145. text-align: center;
  146. font-size: 0.9rem;
  147. width: 100%;
  148. }
  149. .map-shortcuts {
  150. padding: 12px 6px;
  151. }
  152. .map-shortcuts img {
  153. border: solid 1px #000;
  154. width: 75px;
  155. height: 75px;
  156. }
  157. </style>