Code below working fine and draws a polyline
between two points, but what I need is path, so searching how to draw route between these points instead of polyline using vue2-google-maps package?
<template>
<div>
<div>
<h2>Start</h2>
<label>
<gmap-autocomplete @place_changed="setStartPlace"></gmap-autocomplete>
<button @click="addMarker">Add</button>
</label>
<br />
</div>
<div>
<h2>End</h2>
<label>
<gmap-autocomplete @place_changed="setEndPlace"></gmap-autocomplete>
<button @click="addMarker">Add</button>
</label>
<br />
</div>
<br />
<gmap-map ref="xyz" :center="center" :zoom="4" style="width:100%; height: 400px;">
<gmap-marker
:key="index"
v-for="(m, index) in markers"
:position="m.position"
@click="center=m.position"
></gmap-marker>
<gmap-polyline v-bind:path.sync="path" v-bind:options="{ strokeColor:'#008000'}"></gmap-polyline>
</gmap-map>
</div>
</template>
<script>
export default {
name: "GoogleMap",
data() {
return {
// default to Montreal to keep it simple
// change this to whatever makes sense
center: { lat: 45.508, lng: -73.587 },
markers: [],
places: [],
path: [],
currentPlace: null
};
},
mounted() {
this.geolocate();
},
methods: {
// receives a place object via the autocomplete component
setStartPlace(place) {
this.currentPlace = place;
},
setEndPlace(place) {
this.currentPlace = place;
},
addMarker() {
if (this.currentPlace) {
const marker = {
lat: this.currentPlace.geometry.location.lat(),
lng: this.currentPlace.geometry.location.lng()
};
this.path.push(marker);
this.markers.push({ position: marker });
this.places.push(this.currentPlace);
this.center = marker;
this.currentPlace = null;
}
},
geolocate: function() {
navigator.geolocation.getCurrentPosition(position => {
this.center = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
});
}
}
};
</script>
By "Draw route between two points", I assume you mean to use this Directions API.
You can create your own component by using
vue2-google-maps
factory method. See more Adding your own components.Example code:
And then use it:
CodeSandbox Example