I am getting all the result in the console but the route is not getting drawn between the two markers.Should i import any module for the directions? Should i place the directions outside autocomplete?
app.component.ts
i have omitted the autocomplete for source
//load Places Autocomplete2
this.mapsAPILoader.load().then(() => {
let autocomplete = new google.maps.places.Autocomplete(this.searchElementRef1.nativeElement, {
types: ["address"]
});
autocomplete.addListener("place_changed", () => {
this.ngZone.run(() => {
//ge
t the place result
let place: google.maps.places.PlaceResult = autocomplete.getPlace();
//verify result
if (place.geometry === undefined || place.geometry === null) {
return;
}
//set latitude, longitude and zoom
this.latitude1 = place.geometry.location.lat();
this.longitude1 = place.geometry.location.lng();
this.zoom = 9;
});
var map = new google.maps.Map(this.searchElementRef.nativeElement, {
center: {lat:this.latitude,lng:this.longitude},
zoom: 7
});
var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer({
'map': map
});
console.log("display : " + directionsDisplay.getMap().getCenter())
var start = new google.maps.LatLng(this.latitude, this.longitude);
var end = new google.maps.LatLng(this.latitude1, this.longitude1);
// Set destination, origin and travel mode.
var request : google.maps.DirectionsRequest = {
origin: start,
destination: end,
travelMode: google.maps.TravelMode.DRIVING
};
console.log("request : " + request.origin +"request : " + request.destination )
// Pass the directions request to the directions service.
// var directionsService = new google.maps.DirectionsService();
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
// Display the route on the map.
directionsDisplay.setMap(map);
directionsDisplay.setDirections(response);
console.log("response : " + response.geocoded_waypoints.map(f => f.place_id))
} else { console.log("not OK !" + status)}
});
});
});
}
private setCurrentPosition() {
if ("geolocation" in navigator) {
navigator.geolocation.getCurrentPosition((position) => {
this.latitude = position.coords.latitude;
this.longitude = position.coords.longitude;
this.zoom = 10;
});
}
}
}