javascript - Adding InfoWindow on Google directions route -
i trying add infowindow directions route,there lots of examples out there adding infowindow on event listener on marker how can move infowindow show on actual planned route 1 marker another. tried ask question before no response (infowindow on directions route), anyway did lot of googling , found 1 question similar again there no response that. tried infowindow.open(map,this) on event on marker in callback open infowindow on marker position..its want show duration , distance similar google..something in attached image
var infowindow2 = new google.maps.infowindow(); distanceservice.getdistancematrix(distancerequest, function (response, status) { if (status == "ok") { infowindow2.setcontent(response.rows[0].elements[0].distance.text + "<br>" + response.rows[0].elements[0].duration.text + " ") } else { alert("error: " + status) } }) infowindow2.open(map, this);
to find position on route , put infowindow there, parse route (the details described in documentation). location along route , call setposition method of infowindow position.
function calcroute(start, end) { var request = { origin:start, destination:end, travelmode: google.maps.directionstravelmode.driving }; directionsservice.route(request, function(response, status) { if (status == google.maps.directionsstatus.ok) { directionsdisplay.setdirections(response); var step = 1; var infowindow2 = new google.maps.infowindow(); infowindow2.setcontent(response.routes[0].legs[0].steps[step].distance.text + "<br>" + response.routes[0].legs[0].steps[step].duration.text + " "); infowindow2.setposition(response.routes[0].legs[0].steps[step].end_location); infowindow2.open(map); } }); }
code snippet:
var directionsdisplay; var directionsservice = new google.maps.directionsservice(); var map; function initialize() { directionsdisplay = new google.maps.directionsrenderer(); var chicago = new google.maps.latlng(41.850033, -87.6500523); var mapoptions = { zoom: 7, maptypeid: google.maps.maptypeid.roadmap, center: chicago } map = new google.maps.map(document.getelementbyid('map-canvas'), mapoptions); directionsdisplay.setmap(map); calcroute("67 windmill hill, allesley, coventry cv5 9fr, uk", "26 rosaville crescent, allesley, coventry cv5 9bp, uk"); } function calcroute(start, end) { var request = { origin: start, destination: end, travelmode: google.maps.directionstravelmode.driving }; directionsservice.route(request, function(response, status) { if (status == google.maps.directionsstatus.ok) { directionsdisplay.setdirections(response); var step = 1; var infowindow2 = new google.maps.infowindow(); infowindow2.setcontent(response.routes[0].legs[0].steps[step].distance.text + "<br>" + response.routes[0].legs[0].steps[step].duration.text + " "); infowindow2.setposition(response.routes[0].legs[0].steps[step].end_location); infowindow2.open(map); } }); } google.maps.event.adddomlistener(window, 'load', initialize);
html, body, #map-canvas { height: 100%; margin: 0px; padding: 0px } #panel { position: absolute; top: 5px; left: 50%; margin-left: -180px; z-index: 5; background-color: #fff; padding: 5px; border: 1px solid #999; }
<script src="https://maps.googleapis.com/maps/api/js"></script> <div id="map-canvas"></div>
Comments
Post a Comment