javascript - Google Maps Directions Appearing Twice? -
i'm trying create page which, on load, show user's current position on google map. there select list of various possible destinations. when select list changes map show route current position destination. far good!
where goes wrong when try add in driving directions. reason appear twice, can't see why. code:
var latitude = 0; var longitude = 0; $( document ).ready(function() { if (navigator.geolocation) { navigator.geolocation.getcurrentposition(initmap); } else { document.getelementbyid("feedback").innerhtml = "geolocation not supported browser."; } }); function initmap(position) { latitude = position.coords.latitude; longitude = position.coords.longitude; latlong = latitude + "," + longitude; var mycentre = new google.maps.latlng(latitude,longitude); var directionsservice = new google.maps.directionsservice; var directionsdisplay = new google.maps.directionsrenderer; var map = new google.maps.map(document.getelementbyid('map'), { zoom: 14, center: {lat: latitude, lng: longitude}, maptypeid:google.maps.maptypeid.roadmap }); var marker = new google.maps.marker({ position: mycentre, title: 'your current position' }); directionsdisplay.setmap(map); directionsdisplay.setpanel(document.getelementbyid('dirs')); marker.setmap(map); var control = document.getelementbyid('floating-panel'); control.style.display = 'block'; map.controls[google.maps.controlposition.top_center].push(control); var onchangehandler = function() { calculateanddisplayroute(directionsservice, directionsdisplay, mycentre, marker); }; document.getelementbyid('end').addeventlistener('change', onchangehandler); } function calculateanddisplayroute(directionsservice, directionsdisplay, mystart, marker) { marker.setmap(null); directionsservice.route({ origin: mystart, destination: document.getelementbyid('end').value, travelmode: google.maps.travelmode.driving }, function(response, status) { if (status === google.maps.directionsstatus.ok) { directionsdisplay.setdirections(response); } else { window.alert('directions request failed due ' + status); } }); }
i'm guessing has lines:
directionsdisplay.setmap(map); directionsdisplay.setpanel(document.getelementbyid('dirs'));
but can't see what. example on google site (https://developers.google.com/maps/documentation/javascript/examples/directions-panel) has 2 similar lines, doesn't give same issue. i'm needlessly calling multiple times, what?
thanks
the onchange handler being fired twice because inside initmap function. when moved outside initmap function (along variables parameters), worked intended. so, code now:
var latitude = 0; var longitude = 0; $( document ).ready(function() { if (navigator.geolocation) { navigator.geolocation.getcurrentposition(initmap); } else { document.getelementbyid("feedback").innerhtml = "geolocation not supported browser."; } }); var directionservice = ""; var directionsrenderer = ""; var mycentre = ""; var marker = ""; function initmap(position) { latitude = position.coords.latitude; longitude = position.coords.longitude; latlong = latitude + "," + longitude; mycentre = new google.maps.latlng(latitude,longitude); directionsservice = new google.maps.directionsservice; directionsdisplay = new google.maps.directionsrenderer; var map = new google.maps.map(document.getelementbyid('map'), { zoom: 14, center: {lat: latitude, lng: longitude}, maptypeid:google.maps.maptypeid.roadmap }); marker = new google.maps.marker({ position: mycentre, title: 'your current position' }); directionsdisplay.setmap(map); var panel = document.getelementbyid('dirs'); directionsdisplay.setpanel(panel); marker.setmap(map); var control = document.getelementbyid('floating-panel'); control.style.display = 'block'; map.controls[google.maps.controlposition.top_center].push(control); } var onchangehandler = function() { var end = document.getelementbyid('end').value; calculateanddisplayroute(directionsservice, directionsdisplay, mycentre, marker, end); }; document.getelementbyid('end').addeventlistener('change', onchangehandler); function calculateanddisplayroute(directionsservice, directionsdisplay, mystart, marker, end) { marker.setmap(null); directionsservice.route({ origin: mystart, destination: end, travelmode: google.maps.travelmode.driving }, function(response, status) { if (status === google.maps.directionsstatus.ok) { directionsdisplay.setdirections(response); } else { window.alert('directions request failed due ' + status); } }); }
still feels kludge, shoudl able tidy bit.
Comments
Post a Comment