No Description

RouteMap.vue 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <template>
  2. <v-container class="fill-width fill-height">
  3. <v-layout row wrap class="fill-width">
  4. <v-flex xs12 md4 text-xs-center>
  5. <h2>De {{ startPoint.name }} à {{ endPoint.name }}</h2>
  6. <h3>{{ time }}, {{ distance }}</h3>
  7. <ElevationGraph :elevations="elevations"></ElevationGraph>
  8. </v-flex>
  9. <v-flex xs12 md8>
  10. <div class="fill-height fill-width">
  11. <v-map :bounds="bounds">
  12. <v-tilelayer url="http://{s}.tile.osm.org/{z}/{x}/{y}.png"></v-tilelayer>
  13. <v-marker :lat-lng="startPoint.latlng" :autopan="true" :draggable="true" v-on:l-drag="(ev) => { startPoint.latlng = ev.latlng; startPoint.isDragged = true; }" v-on:l-dragend="getRoutes"></v-marker>
  14. <v-marker :lat-lng="endPoint.latlng" :autopan="true" :draggable="true" v-on:l-drag="(ev) => { endPoint.latlng = ev.latlng; endPoint.isDragged = true; }" v-on:l-dragend="getRoutes"></v-marker>
  15. <v-polyline v-if="latLngs" :latLngs="latLngs"></v-polyline>
  16. <v-polyline v-if="startLatLngs" :latLngs="startLatLngs" dashArray="5, 5"></v-polyline>
  17. <v-polyline v-if="endLatLngs" :latLngs="endLatLngs" dashArray="5, 5"></v-polyline>
  18. </v-map>
  19. </div>
  20. </v-flex>
  21. </v-layout>
  22. </v-container>
  23. </template>
  24. <script>
  25. // TODO: Check latitude and longitude are correct when entering this component.
  26. import L from 'leaflet';
  27. import iconRetinaUrl from 'leaflet/dist/images/marker-icon-2x.png';
  28. import iconUrl from 'leaflet/dist/images/marker-icon.png';
  29. import shadowUrl from 'leaflet/dist/images/marker-shadow.png';
  30. import polyline from '@mapbox/polyline';
  31. import prettyMetric from 'pretty-metric';
  32. import { getRoutes } from '../api';
  33. import ElevationGraph from './ElevationGraph.vue';
  34. // Fix for a bug in Leaflet default icon
  35. // see https://github.com/PaulLeCam/react-leaflet/issues/255#issuecomment-261904061
  36. delete L.Icon.Default.prototype._getIconUrl;
  37. L.Icon.Default.mergeOptions({
  38. iconRetinaUrl,
  39. iconUrl,
  40. shadowUrl,
  41. });
  42. export default {
  43. components: {
  44. ElevationGraph,
  45. },
  46. created() {
  47. this.initializePoints();
  48. this.getRoutes();
  49. },
  50. watch: {
  51. $route: 'initializePoints',
  52. },
  53. data() {
  54. return {
  55. startPoint: {
  56. latlng: null,
  57. isDragged: false,
  58. name: null,
  59. },
  60. endPoint: {
  61. latlng: null,
  62. isDragged: false,
  63. name: null,
  64. },
  65. route: null,
  66. };
  67. },
  68. methods: {
  69. initializePoints() {
  70. this.startPoint.latlng = L.latLng(
  71. this.$route.params.start_point.split(',').map(item => parseFloat(item)),
  72. );
  73. this.endPoint.latlng = L.latLng(
  74. this.$route.params.end_point.split(',').map(item => parseFloat(item)),
  75. );
  76. },
  77. getRoutes() {
  78. this.startPoint.name = 'TODO';
  79. this.endPoint.name = 'TODO';
  80. getRoutes(
  81. this.startPoint.latlng,
  82. this.endPoint.latlng,
  83. this.$route.params.transport_modes,
  84. )
  85. .then((routes) => {
  86. this.startPoint.isDragged = false;
  87. this.endPoint.isDragged = false;
  88. this.route = routes[0];
  89. });
  90. },
  91. },
  92. computed: {
  93. elevations() {
  94. if (!this.route) {
  95. return [];
  96. }
  97. return this.route.elevations;
  98. },
  99. bounds() {
  100. if (this.route && this.route.bbox) {
  101. return [
  102. [
  103. this.route.bbox[1],
  104. this.route.bbox[0],
  105. ],
  106. [
  107. this.route.bbox[3],
  108. this.route.bbox[2],
  109. ],
  110. ];
  111. }
  112. return [
  113. [
  114. Math.min(this.startPoint.latlng.lat, this.endPoint.latlng.lat),
  115. Math.min(this.startPoint.latlng.lng, this.endPoint.latlng.lng),
  116. ],
  117. [
  118. Math.max(this.startPoint.latlng.lat, this.endPoint.latlng.lat),
  119. Math.max(this.startPoint.latlng.lng, this.endPoint.latlng.lng),
  120. ],
  121. ];
  122. },
  123. latLngs() {
  124. if (!this.route) {
  125. return null;
  126. }
  127. return polyline.decode(this.route.geojson);
  128. },
  129. startLatLngs() {
  130. if (!this.latLngs || this.startPoint.isDragged) {
  131. return null;
  132. }
  133. return [this.startPoint.latlng, this.latLngs[0]];
  134. },
  135. endLatLngs() {
  136. if (!this.latLngs || this.endPoint.isDragged) {
  137. return null;
  138. }
  139. return [this.latLngs[this.latLngs.length - 1], this.endPoint.latlng];
  140. },
  141. time() {
  142. if (!this.route) {
  143. return null;
  144. }
  145. return this.route.duration.humanize();
  146. },
  147. distance() {
  148. if (!this.route) {
  149. return null;
  150. }
  151. return prettyMetric(this.route.distances.bike).humanize();
  152. },
  153. },
  154. };
  155. </script>
  156. <style>
  157. .application .leaflet-bar a {
  158. color: black;
  159. }
  160. </style>
  161. <style scoped>
  162. .fill-width {
  163. width: 100%;
  164. max-width: 100%;
  165. }
  166. .fill-height {
  167. height: 100%;
  168. }
  169. </style>