Advertisement
JMarkin

Untitled

Aug 18th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. <template>
  3.  
  4.   <q-page class="flex flex-center">
  5.     <div id="map_canvas">
  6.       <button id="button">Click me!</button>
  7.     </div>
  8.     <p>{{center}}</p>
  9.     <gmap-map
  10.       :zoom="7"
  11.       :center="center"
  12.       style="width: 500px; height: 300px"
  13.       ref="map"
  14.     >
  15.       <gmap-marker
  16.         :key="index"
  17.         v-for="(m, index) in markers"
  18.         :position="m.position"
  19.         :clickable="true"
  20.         :draggable="true"
  21.         @click="center=m.position"
  22.       ></gmap-marker>
  23.     </gmap-map>
  24.     <q-btn @click="route()">
  25.       route
  26.     </q-btn>
  27.   </q-page>
  28. </template>
  29.  
  30. <style>
  31. </style>
  32.  
  33. <script>
  34. /* eslint-disable no-unused-vars */
  35. export default {
  36.   name: 'PageIndex',
  37.   data: () => ({
  38.     markers: [{
  39.       position: {lat: 10.0, lng: 10.0}
  40.     }, {
  41.       position: {lat: 11.0, lng: 11.0}
  42.     }],
  43.     directionsDisplay: {},
  44.     directionsService: {}
  45.   }),
  46.   created () {
  47.     console.log(this.center)
  48.   },
  49.   methods: {
  50.     route () {
  51.       this.directionsService = new this.google.maps.DirectionsService()
  52.       this.directionsDisplay = new this.google.maps.DirectionsRenderer()
  53.       console.log(this.$refs.map.$mapObject)
  54.       this.directionsDisplay.setMap(this.$refs.map.$mapObject)
  55.       console.log(this.directionsDisplay.map)
  56.       this.directionsService.route({
  57.         origin: this.markers[0].position, // Can be coord or also a search query
  58.         destination: this.markers[1].position,
  59.         travelMode: 'DRIVING'
  60.       }, (response, status) => {
  61.         if (status === 'OK') {
  62.           this.directionsDisplay.setDirections(response) // draws the polygon to the map
  63.         } else {
  64.           console.log('Directions request failed due to ' + status)
  65.         }
  66.       })
  67.     }
  68.   },
  69.   asyncComputed: {
  70.     center: {
  71.       get () {
  72.         return new Promise((resolve) => {
  73.           navigator.geolocation.getCurrentPosition(position => {
  74.             resolve({
  75.               lat: position.coords.latitude,
  76.               lng: position.coords.longitude
  77.             })
  78.           })
  79.         })
  80.       },
  81.       default: {lat: 11.0, lng: 11.0}
  82.     }
  83.   }
  84. }
  85. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement