Yogendra Sahu

Yogendra Sahu

  • 1.2k
  • 587
  • 138.1k

Google Map setDirections(response) not showing path

Aug 3 2018 6:04 AM
I am implementing google map in Angular 5 using npm library googlemaps.
 
When trying to show route/path between 2 points using setDirections(),I am unable to see the path.The 2 points are shown but no route is drawn and there is even no error in console.
 
Following is the code use for showing route:
  1. var mapProp = {  
  2. center: new google.maps.LatLng(22.0824, 82.1411),  
  3. zoom: 15,  
  4. mapTypeId: google.maps.MapTypeId.ROADMAP  
  5. };  
  6.    
  7. this.map = new google.maps.Map(this.gmapElement.nativeElement, mapProp);  
  8. this.directionsDisplay.setMap(this.map);  
  9. this.marker1 = new google.maps.Marker({  
  10. map: this.map,  
  11. draggable: true,  
  12. position: { lat: 22.0717, lng: 82.1497 }  
  13. });  
  14. this.marker2 = new google.maps.Marker({  
  15. map: this.map,  
  16. draggable: true,  
  17. position: { lat: 22.0942, lng: 82.1257 }  
  18. });  
  19.    
  20. google.maps.event.addListener(this.marker1, 'position_changed'this.update);  
  21. google.maps.event.addListener(this.marker2, 'position_changed'this.update);  
  22.    
  23. this.lat1 = 22.0717;  
  24. this.lang1 = 82.1497;  
  25. this.lat2 = 22.0942;  
  26. this.lang2 = 82.1257;  
  27. this.Source = new google.maps.LatLng(this.lat1, this.lang1);  
  28. this.Destination = new google.maps.LatLng(this.lat2, this.lang2);  
  29.    
  30. var request = {  
  31. origin: this.Source,  
  32. destination: this.Destination,  
  33. travelMode: google.maps.TravelMode.DRIVING  
  34. }  
  35.    
  36. this.directionsService.route(request, function (response, status) {  
  37. if (status === google.maps.DirectionsStatus.OK) {  
  38. new google.maps.DirectionsRenderer().setDirections(response);  
  39. else  
  40.    {  
  41. window.alert('Directions request failed due to ' + status);  
  42.    }  
  43. });  
  44. }  

Answers (2)