운전 방향을 보여주는 자동 완성 입력란이있는 Google지도가 있습니다. 문제는 방향 서비스가 자동 완성 된 변수 대신 입력 변수를 사용하기 때문에 오류가 발생한다는 것입니다. 이 문제를 어떻게 해결할 수 있습니까?자동 완성 상자가있는 google지도의 운전 방향
데모 : http://touristification.com/reisadvies/routekaart.html
<div id="map"></div>
<script>
var placeSearch, autocomplete, autocomplete2;
google.maps.event.addDomListener(window, 'load', initMap);
function initMap() {
var input1 = document.getElementById('locationTextField');
var start = new google.maps.places.Autocomplete(input1);
var input2 = document.getElementById('locationTextField2');
var end = new google.maps.places.Autocomplete(input2);
var directionsService = new google.maps.DirectionsService;
var directionsDisplay = new google.maps.DirectionsRenderer;
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 7,
center: {lat: 52.34, lng: 4.87}
});
directionsDisplay.setMap(map);
var geocoder = new google.maps.Geocoder;
var onChangeHandler = function() {
calculateAndDisplayRoute(directionsService, directionsDisplay);
};
document.getElementById('locationTextField').addEventListener('change', onChangeHandler);
document.getElementById('locationTextField2').addEventListener('change', onChangeHandler);
}
function calculateAndDisplayRoute(directionsService, directionsDisplay) {
directionsService.route({
origin: document.getElementById('locationTextField').value,
destination: document.getElementById('locationTextField2').value,
travelMode: 'DRIVING'
}, function(response, status) {
if (status === 'OK') {
directionsDisplay.setDirections(response);
} else {
window.alert('Directions request failed due to ' + status);
}
});
}
</script>
아니라 서둘러에서 이루어졌다 예상치 못한 오류가 발생할 수 있습니다. " – bora89
그래도 작동하지 않으면 매개 변수로 start 및 end를 calculateAndDisplayRoute에 전달하고 destination의 origin.geometry.location에 start.geometry.location과 함께 directionService를 사용하십시오. – Narayon
감사. 수정되었지만 여전히 오류가 있습니다. – Tim