Structures.vue 5.9 KB

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