Creative Enemy
Creative Enemy

Reputation: 398

Google Map - get direction to place and find traffic on that particular route using API

I am trying to using Google map direction identifier. with traffic details.

I get direction, but not get traffic details for particular selected path. it apply overall traffic.

 html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
      #map {
        height: 100%;
      }
      #floating-panel {
        position: absolute;
        top: 10px;
        left: 25%;
        z-index: 5;
        background-color: #fff;
        padding: 5px;
        border: 1px solid #999;
        text-align: center;
        font-family: 'Roboto','sans-serif';
        line-height: 30px;
        padding-left: 10px;
      }
 <div id="floating-panel">
    <b>Start: </b>
    <select id="start">
      <option value="coimbatore, in">coimbatore</option>
      <option value="tirupur, in">St Louis</option>
      <option value="joplin, mo">Joplin, MO</option>
      <option value="oklahoma city, ok">Oklahoma City</option>
      <option value="amarillo, tx">Amarillo</option>
      <option value="gallup, nm">Gallup, NM</option>
      <option value="flagstaff, az">Flagstaff, AZ</option>
      <option value="winona, az">Winona</option>
      <option value="kingman, az">Kingman</option>
      <option value="barstow, ca">Barstow</option>
      <option value="san bernardino, ca">San Bernardino</option>
      <option value="los angeles, ca">Los Angeles</option>
    </select>
    <b>End: </b>
    <select id="end">
      <option value="chicago, il">Chicago</option>
      <option value="tirupur, in">tirupur</option>
      <option value="joplin, mo">Joplin, MO</option>
      <option value="oklahoma city, ok">Oklahoma City</option>
      <option value="amarillo, tx">Amarillo</option>
      <option value="gallup, nm">Gallup, NM</option>
      <option value="flagstaff, az">Flagstaff, AZ</option>
      <option value="winona, az">Winona</option>
      <option value="kingman, az">Kingman</option>
      <option value="barstow, ca">Barstow</option>
      <option value="san bernardino, ca">San Bernardino</option>
      <option value="los angeles, ca">Los Angeles</option>
    </select>
    </div>
    <div id="map"></div>
    <script>
      function initMap() {
        var directionsService = new google.maps.DirectionsService;
        var directionsDisplay = new google.maps.DirectionsRenderer;
        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 15,
          center: {lat:11.0203840, lng: 76.9160980}
        });
        directionsDisplay.setMap(map);

        var onChangeHandler = function() {
          calculateAndDisplayRoute(directionsService, directionsDisplay);
        };
	var trafficLayer = new google.maps.TrafficLayer();
        trafficLayer.setMap(map);

        document.getElementById('start').addEventListener('change', onChangeHandler);
        document.getElementById('end').addEventListener('change', onChangeHandler);
      }

      function calculateAndDisplayRoute(directionsService, directionsDisplay) {
        directionsService.route({
          origin: document.getElementById('start').value,
          destination: document.getElementById('end').value,
          travelMode: 'DRIVING'
        }, function(response, status) {
          if (status === 'OK') {
            directionsDisplay.setDirections(response);
          } else {
            window.alert('Directions request failed due to ' + status);
          }
        });
      }
    </script>
    <script async defer
    src="https://maps.googleapis.com/maps/api/js?sensor=false&callback=initMap">
    </script>

Upvotes: 0

Views: 120

Answers (1)

mxlse
mxlse

Reputation: 2784

It is not possible to render the traffic information just for the certain route or receive the raw traffic data for a route with Google Maps API. Your only option is the google.maps.TrafficLayer for the whole bounding box - as you are already using it.

You can get travel times for a route taking traffic into consideration by Google APIs, but nothing more. If you could receive it somehow it could violate the terms of service of Google Maps, but I'm not sure about this.

Perhaps the Bing Maps REST Services - Traffic API could help you to receive some data, but I'm not familiar with this one and due to that not sure if it only gives data about "incidents" or also general traffic.

Upvotes: 1

Related Questions