Google Map Add Directions service from one place to another

Google Map Add Directions service from one place to another

You can calculate directions between start and the end point with the help of this Google map service. Google Maps Directions Service receives direction requests and returns computed results. The provided service is also helpful for the developers who want to compute direction data within maps given by one of the Google Maps APIs. This service can return multi-part directions using a series of waypoints.

Add style in your head tag.

  1. <style>
  2. html, body {
  3. height: 100%;
  4. margin: 0;
  5. padding: 0;
  6. }
  7. #map {
  8. height: 99%;
  9. width: 99%;
  10. }
  11. #panel {
  12. position: absolute;
  13. top: 0px;
  14. left: 25%;
  15. z-index: 5;
  16. background-color: #fff;
  17. padding: 0px;
  18. border: 1px solid #999;
  19. text-align: center;
  20. font-family: 'Roboto','sans-serif';
  21. line-height: 0px;
  22. padding-left: 10px;
  23. }
  24. </style>
Add script before closing head tag.

  1. <script src="https://maps.googleapis.com/maps/api/js?signed_in=true&callback=initMap&key={key}"
  2. async defer></script>
  3. <script>
  4. function initMap() {
  5. var lat_lng = {lat: 22.08672, lng: 79.42444};
  6. var directionsService = new google.maps.DirectionsService;
  7. var directionsDisplay = new google.maps.DirectionsRenderer;
  8. var map = new google.maps.Map(document.getElementById('map'), {
  9. zoom: 6,
  10. center: lat_lng
  11. });
  12. directionsDisplay.setMap(map);
  13. var onChangeHandler = function() {
  14. calculateAndDisplayRoute(directionsService, directionsDisplay);
  15. };
  16. document.getElementById('Source').addEventListener('change', onChangeHandler);
  17. document.getElementById('Destination').addEventListener('change', onChangeHandler);
  18. }
  19. function calculateAndDisplayRoute(directionsService, directionsDisplay) {
  20. directionsService.route({
  21. origin: document.getElementById('Source').value,
  22. destination: document.getElementById('Destination').value,
  23. travelMode: google.maps.TravelMode.DRIVING
  24. }, function(response, status) {
  25. if (status === google.maps.DirectionsStatus.OK) {
  26. directionsDisplay.setDirections(response);
  27. } else {
  28. window.alert('Request for getting direction is failed due to ' + status);
  29. }
  30. });
  31. }
  32. </script>
HTML File

  1. <div id="panel">
  2. <b>Source: </b>
  3. <select id="Source">
  4. <option value="New Delhi, in">New Delhi</option>
  5. <option value="Mumbai, in">Mumbai</option>
  6. <option value="Kolkata, in">Kolkata</option>
  7. <option value="Chennai, in">Chennai</option>
  8. <option value="Indore, in">Indore</option>
  9. <option value="Hyderabad, in">Hyderabad</option>
  10. <option value="Ahemdabad, in">Ahemdabad</option>
  11. <option value="Pune, in">Pune</option>
  12. <option value="Bhopal, in">Bhopal</option>
  13. <option value="Jaipur, in">Jaipur</option>
  14. <option value="Bengluru, in">Bengluru</option>
  15. </select>
  16. <b>Destination: </b>
  17. <select id="Destination">
  18. <option value="Mumbai, in">Mumbai</option>
  19. <option value="New Delhi, in">New Delhi</option>
  20. <option value="Kolkata, in">Kolkata</option>
  21. <option value="Chennai, in">Chennai</option>
  22. <option value="Indore, in">Indore</option>
  23. <option value="Ahemdabad, in">Ahemdabad</option>
  24. <option value="Jaipur, in">Jaipur</option>
  25. <option value="Bengluru, in">Bengluru</option>
  26. <option value="Hyderabad, in">Hyderabad</option>
  27. <option value="Pune, in">Pune</option>
  28. <option value="Bhopal, in">Bhopal</option>
  29. </select>
  30. </div>
  31. <div id="map"></div>

Phone: (+91) 8800417876
Noida, 201301