i using javascript api show google map directions (based on user input) along traffic layer. works well, cannot figure out way show traffic colors on relevant polylines above custom created line. what's correct way this?
displaying traffic layer
simply displaying traffic layer can done using javascript api following
function initmap() { var map = new google.maps.map(document.getelementbyid('map'), { zoom: 13, center: {lat: 34.04924594193164, lng: -118.24104309082031} }); var trafficlayer = new google.maps.trafficlayer(); trafficlayer.setmap(map); } this not give access traffic layer, transit , bicycling layers well.
here working example:
<!doctype html> <html> <head> <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> <meta charset="utf-8"> <title>traffic layer</title> <style> html, body { height: 100%; margin: 0; padding: 0; } #map { height: 100%; } </style> </head> <body> <div id="map"></div> <script> function initmap() { var map = new google.maps.map(document.getelementbyid('map'), { zoom: 13, center: {lat: 34.04924594193164, lng: -118.24104309082031} }); var trafficlayer = new google.maps.trafficlayer(); trafficlayer.setmap(map); } </script> <script async defer src="https://maps.googleapis.com/maps/api/js?key=your_api_key&callback=initmap"> </script> </body> </html> ps: before using make sure use api key in
<script async defer src="https://maps.googleapis.com/maps/api/js?key=your_api_key&callback=initmap"> </script> for more information example can read documentation page regarding example or full documentation displaying data (which recomemend).
this not want
experience usualy tells when suggesting route user, if said route has red traffic line user automatically search else - forcing him query cumbersome.
thus traffic layers solution avoid previous behavior.
this means can't pick part of layer (say, 1 matching polyline) , paste there - not purpose of layers.
if solution not you, there way...
directions service
the directions service caluculates directions between 2 points.
with service, can make request provideroutealternatives set true , departuretime set drivingoptions.
according documentation,
departuretime(required drivingoptions object literal valid) specifies desired time of departure date object. (...) google maps apis premium plan customers, if include departuretime in request, api returns best route given expected traffic conditions @ time, , includes predicted time in traffic (duration_in_traffic) in response. (...)
so if make request alternative routes , have departuretime have duration_in_traffic in reponse each route, , can draw polygon colour want depending on how or bad path is.
you can read more javascript directions service @ https://developers.google.com/maps/documentation/javascript/directions
this still not want
using google maps apis , services, far can go. if these options still don't suit need mix map traffic data third-parties.
hope helps!
Comments
Post a Comment