Structures.vue 5.1 KB

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