Structures.vue 5.1 KB

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