Google Maps Travelling Mode Using Bootstrap In ASP.NET MVC - PART Seven

Introduction

The use of the DirectionsService object is to fetch the directions for different travel modes.

Travel Modes


When you calculate directions, you need to specify which transportation mode you are willing to use. The following travel modes are currently available.
  1. DRIVING - (Default) indicates standard driving directions using the road network.
  2. BICYCLING - requests bicycling directions via bicycle paths & preferred streets.
  3. TRANSIT  - requests directions via public transit routes.
  4. WALKING - requests walking directions via pedestrian paths & sidewalks.
Consult the Google Maps coverage data to determine, to what extent a country supports directions. If you request directions for a region in which that direction type is not available, the response will return the DirectionsStatus="ZERO_RESULTS".

Walking directions may not include clear pedestrian paths, so walking directions will return warnings in the DirectionsResult which you must display if you are not using the default DirectionsRenderer.

Steps

Before you go through this blog, please go through my Google Maps related blogs from PART-1 to PART-6.
 
Now, let's continue.
 
First, create a Controller action method named "ModeTravel" in "HomeController.cs". 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6.   
  7. namespace SatyaGoogleMapBootstrapMVC.Controllers  
  8. {  
  9.     public class HomeController : Controller  
  10.     {  
  11.         //  
  12.         // GET: /Home/  
  13.   
  14.         public ActionResult Index()  
  15.         {  
  16.             return View();  
  17.         }  
  18.         public ActionResult Details()  
  19.         {  
  20.             return View();  
  21.         }  
  22.         public ActionResult Animate()  
  23.         {  
  24.             return View();  
  25.         }  
  26.         public ActionResult Icon()  
  27.         {  
  28.             return View();  
  29.         }  
  30.         public ActionResult Steet()  
  31.         {  
  32.             return View();  
  33.         }  
  34.         public ActionResult ModeTravel()  
  35.         {  
  36.             return View();  
  37.         }  
  38.     }  
  39. }   
Create a View named "ModeTravel.cshtml".
  1. @{  
  2.     ViewBag.Title = "Satyaprakash Google Map Travelling Mode";  
  3. }  
  4.   
  5. <title>@ViewBag.Title</title>  
  6.   
  7. <h2 style="background-color: Yellow;color: Blue; text-align: center; font-style: oblique">Satyaprakash's Google Map Travelling Mode Using MVC and BOOTSTRAP</h2>  
  8.     <meta charset="utf-8">  
  9.     <meta name="viewport" content="width=device-width, initial-scale=1">  
  10.     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">  
  11.     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>  
  12.     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>  
  13.   
  14.    <style>  
  15.      
  16.     #map {  
  17.         height: 50%;  
  18.     }  
  19.  
  20.     #Dropdown {  
  21.         top: 10px;  
  22.         left: 25%;  
  23.         z-index: 5;  
  24.         background-color:orange;  
  25.         padding: 5px;  
  26.         border: 1px solid blue;  
  27.         text-align: center;  
  28.         font-family: 'Roboto','sans-serif';  
  29.         line-height: 30px;  
  30.         padding-left: 10px;  
  31.     }  
  32. </style>  
  33.     <div id="Dropdown">  
  34.     <b style="color:blue">Mode Of Visting: </b>  
  35.     <select id="mode">  
  36.         <option value="DRIVING">By Driving</option>  
  37.         <option value="WALKING">By Walking</option>  
  38.         <option value="BICYCLING">By Bicycling</option>  
  39.         <option value="TRANSIT">Transit Or Bus Availability</option>  
  40.     </select>  
  41. </div>  
  42.     <div id="map"></div>  
  43.     <script>  
  44.         function initMap() {  
  45.             var directionsDisplay = new google.maps.DirectionsRenderer;  
  46.             var directionsService = new google.maps.DirectionsService;  
  47.             var map = new google.maps.Map(document.getElementById('map'), {  
  48.                 zoom: 11,  
  49.                 center: { lat: 12.9081, lng: 77.6476 }  
  50.             });  
  51.               
  52.             directionsDisplay.setMap(map);  
  53.              
  54.             calculateAndDisplayRoute(directionsService, directionsDisplay);  
  55.             document.getElementById('mode').addEventListener('change'function () {  
  56.                 calculateAndDisplayRoute(directionsService, directionsDisplay);  
  57.             });  
  58.         }  
  59.   
  60.         function calculateAndDisplayRoute(directionsService, directionsDisplay) {  
  61.             var selectedMode = document.getElementById('mode').value;  
  62.             directionsService.route({  
  63.                 origin: { lat: 12.9081, lng: 77.6476 },  // Hsr Layout.  
  64.                 destination: { lat: 12.9592, lng: 77.6974 },  // marathahalli.  
  65.   
  66.                 travelMode: google.maps.TravelMode[selectedMode]  
  67.   
  68.             }, function (response, status) {  
  69.                 if (status == 'OK') {  
  70.                     directionsDisplay.setDirections(response);  
  71.                 } else {  
  72.                     window.alert('Directions Query Unavailable Due To ' + status);  
  73.                 }  
  74.                  
  75.             });  
  76.         }  
  77.     </script>  
  78.   
  79.     <script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkVZYQFe4YYva_g5ulymGDt9EBoVjjZJ8&callback=initMap"></script>  
  80.   
  81.     <footer>  
  82.         <p style="background-color: Yellow; font-weight: bold; color:blue; text-align: center; font-style: oblique">© @DateTime.Now.ToLocalTime()</p> @*Add Date Time*@  
  83.     </footer> 
 I added a dropdown with modes of travelling ...
  1. <select id="mode">  
  2.         <option value="DRIVING">By Driving</option>  
  3.         <option value="WALKING">By Walking</option>  
  4.         <option value="BICYCLING">By Bicycling</option>  
  5.         <option value="TRANSIT">Transit Or Bus Availability</option>  
  6.     </select> 
 You can calculate the directions (using a variety of methods of transportation) by using the DirectionsService object. This object communicates with the Google Maps API Directions Service which receives direction request and returns an efficient path.

You may either handle these direction results yourself or use the DirectionsRenderer object to render the same.
  1. var directionsDisplay = new google.maps.DirectionsRenderer;  
  2. var directionsService = new google.maps.DirectionsService;   
The Directions Service can return multi-part directions using a series of waypoints. Directions are displayed as a polyline drawing the route on a map, or additionally as a series of textual description within a <div> element.

Here, I have called a function to calculate and show the routes.
  1. calculateAndDisplayRoute(directionsService, directionsDisplay); 
Then, after selecting any one option in dropdown, the function will work.
  1. document.getElementById('mode').addEventListener('change'function () {  
  2.                 calculateAndDisplayRoute(directionsService, directionsDisplay);  
  3.             }); 
I have defined and built the function here.
  1. function calculateAndDisplayRoute(directionsService, directionsDisplay) {  
  2.             var selectedMode = document.getElementById('mode').value;  
  3.             directionsService.route({  
  4.                 origin: { lat: 12.9081, lng: 77.6476 },  // For Hsr Layout.  
  5.                 destination: { lat: 12.9592, lng: 77.6974 },  // For marathahalli.  
  6.   
  7.                  
  8.                 travelMode: google.maps.TravelMode[selectedMode]  
  9.   
  10.             }, function (response, status) {  
  11.                 if (status == 'OK') {  
  12.                     directionsDisplay.setDirections(response);  
  13.                 } else {  
  14.                     window.alert('Directions Query Unavailable Due To ' + status);  
  15.                 }  
  16.                  
  17.             });  
  18.         } 
Note that JavaScript allows us to access the constant using square brackets and a string value as its "property".
  1. travelMode: google.maps.TravelMode[selectedMode] 
Then, if any mode of option is not available between the places , then the below mentioned steps will be executed.
  1. function (response, status) {  
  2.                 if (status == 'OK') {  
  3.                     directionsDisplay.setDirections(response);  
  4.                 } else {  
  5.                     window.alert('Directions Query Unavailable Due To ' + status);  
  6.                 }   
OUTPUT

Desktop View
 
 
 
 
Mobile View
 
 
 
 
NOTE

For BICYCLING mode,the HSR Layout and Marathahalli route are not found on Google Server.

For TRANSIT mode , you can get the details of available buses with bus no. and other infromation.

Summary

Today, we learned the following.
  1. What Google Maps Travelling Mode is.
  2. How to implement it in MVC and Bootstrap.
  3. Mode of travelling for some places are not fed to Google Server.