Structures.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. zoomAtStart: {
  62. type: Boolean,
  63. required: false,
  64. default: false
  65. }
  66. },
  67. data () {
  68. return {
  69. map: null,
  70. defaultBounds: new L.LatLngBounds([51.03, -5.78], [41.2, 9.70]),
  71. shortcuts: [
  72. { src: '/images/metropole.png', alt: 'Metropole', bounds: [[51.03, -5.78], [41.2, 9.70]] },
  73. { src: '/images/guadeloupe.png', alt: 'Guadeloupe', bounds: [[16.62, -62.03], [15.74, -60.97]] },
  74. { src: '/images/martinique.png', alt: 'Martinique', bounds: [[14.95, -61.43], [14.28, -60.60]] },
  75. { src: '/images/mayotte.png', alt: 'Mayotte', bounds: [[-12.51, 44.86], [-13.19, 45.45]] },
  76. { src: '/images/la_reunion.png', alt: 'La Réunion', bounds: [[-20.65, 54.92], [-21.65, 56.15]] },
  77. { src: '/images/guyane.png', alt: 'Guyane', bounds: [[6.24, -54.62], [1.87, -50.59]] }
  78. ],
  79. zoomRequired: this.zoomAtStart
  80. }
  81. },
  82. watch: {
  83. structures (oldVal: Array<Structure>, newVal: Array<Structure>): void {
  84. if (oldVal !== newVal) {
  85. this.populateMarkers()
  86. }
  87. if (this.zoomRequired) {
  88. this._applyZoom()
  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. if (this.zoomRequired) {
  106. this._applyZoom()
  107. }
  108. },
  109. beforeDestroy (): void {
  110. if (this.map) {
  111. this.map.remove()
  112. }
  113. },
  114. methods: {
  115. populateMarkers (): void {
  116. // remove any existant marker layers
  117. this.map.eachLayer((layer: L.Layer) => {
  118. if (layer instanceof L.MarkerClusterGroup) {
  119. this.map.removeLayer(layer)
  120. }
  121. })
  122. // populate new layer
  123. const clusters = L.markerClusterGroup()
  124. for (const s of this.structures) {
  125. if (!s.latitude || !s.longitude) { continue }
  126. const marker = L.marker([s.latitude, s.longitude])
  127. marker.bindPopup(`<b>${s.name}</b><br/>${s.postalCode} ${s.addressCity}<br/><a href="${s.website}" target="_blank">${s.website}</a>`)
  128. clusters.addLayer(marker)
  129. }
  130. this.map.addLayer(clusters)
  131. },
  132. setMapBounds (bounds: L.LatLngBoundsExpression, padding: [number, number] = [0, 0]): void {
  133. this.map.fitBounds(bounds, { padding }) // << without this, the new bounds may not be properly set when the current view overlaps the new bounds.
  134. },
  135. resetBounds (): void {
  136. this.setMapBounds(this.defaultBounds)
  137. },
  138. zoomOnResults (): void {
  139. this.zoomRequired = true
  140. },
  141. _fitBoundsToMarkers (): boolean {
  142. const coords: L.LatLngBoundsLiteral = this.structures.filter(
  143. (s: Structure) => {
  144. return (
  145. s.latitude && typeof s.latitude !== 'undefined' &&
  146. s.longitude && typeof s.latitude !== 'undefined'
  147. )
  148. }
  149. ).map(
  150. (s: Structure) => { return [s.latitude as number, s.longitude as number] as L.LatLngTuple }
  151. )
  152. if (!(coords.length > 0)) {
  153. return false
  154. }
  155. this.setMapBounds(coords, [80, 80])
  156. return true
  157. },
  158. _applyZoom (): void {
  159. const zoomed = this._fitBoundsToMarkers()
  160. if (!zoomed) {
  161. this.setMapBounds(this.defaultBounds)
  162. }
  163. this.zoomRequired = false
  164. }
  165. }
  166. })
  167. </script>
  168. <style scoped lang="scss">
  169. #map {
  170. height: 100%;
  171. width: 100%;
  172. }
  173. .advice {
  174. margin: 1rem 0;
  175. color: #262626;
  176. font-weight: 750;
  177. text-align: center;
  178. font-size: 0.9rem;
  179. width: 100%;
  180. }
  181. .map-shortcuts {
  182. padding: 12px 6px;
  183. }
  184. .map-shortcut {
  185. position: relative;
  186. border: solid 1px #000;
  187. height: 87px;
  188. width: 87px;
  189. }
  190. .map-shortcut-overlay {
  191. position: absolute;
  192. top: 0;
  193. bottom: 0;
  194. left: 0;
  195. right: 0;
  196. height: 100%;
  197. width: 100%;
  198. opacity: 0;
  199. cursor: pointer;
  200. }
  201. .map-shortcut:hover .map-shortcut-overlay {
  202. opacity: 0.5;
  203. }
  204. .map-shortcut-overlay>.icon {
  205. color: #595959;
  206. font-size: 38px;
  207. position: absolute;
  208. top: 50%;
  209. left: 50%;
  210. -webkit-transform: translate(-50%, -50%);
  211. -ms-transform: translate(-50%, -50%);
  212. transform: translate(-50%, -50%);
  213. text-align: center;
  214. }
  215. </style>