車の運転ルートを示す入力フィールドが自動入力されたGoogleマップがあります。問題は、directionserviceがオートコンプリートされた変数の代わりに入力変数を使用するため、エラーが発生することです。これをどうすれば解決できますか?オートコンプリートボックスを使用した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およびend.geometry.locationにstart.geometry.locationを指定してdirectionServiceを使用してみてください。 – Narayon
ありがとうございます。修正済みですが、まだエラーがあります。 – Tim