Для начала я введу источник и пункт назначения, и он покажет отображаемый маршрут от пункта A до пункта B. Однако я хотел бы сделать источник A перетаскиваемым, чтобы он пересчитывал маршрут и отображал для меня,
var directionsDisplay = new google.maps.DirectionsRenderer;
var directionsService = new google.maps.DirectionsService;
var map = new google.maps.Map(document.getElementById('map'), {
mapTypeControl: false,
center: {lat: 1.317206, lng: 103.772240},
zoom: 13
});
new AutocompleteDirectionsHandler(map);
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById('right-panel'));
var onChangeHandler = function() {
calculateAndDisplayRoute(directionsService, directionsDisplay);
};
document.getElementById('destination-input').addEventListener('change',
onChangeHandler);
}
function calculateAndDisplayRoute(directionsService,
directionsDisplay) {
var start = document.getElementById('origin-input').value;
var end = document.getElementById('destination-input').value;
directionsService.route({
origin: start,
destination: end,
travelMode: 'DRIVING'
}, function(response, status) {
if (status === 'OK') {
directionsDisplay.setDirections(response);
}
});
}
HTML-код приведен ниже.
<body>
<input id="origin-input" class="controls" type="text"
placeholder="Enter an origin location">
<input id="destination-input" class="controls" type="text"
placeholder="Enter a destination location">
<div id="map"></div>
</body>
Ниже приведен скриншот текущего проекта, который у меня есть. Скриншот