Structures.vue 5.9 KB

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