我对使用Javascript的Google API方向有疑问,我是第一次使用它。
我正在开发一个拼车网站。我想在使用Google自动填充输入的两个地方之间创建一个方向。我设法获得了我作为字符串传递的两个LatLng坐标,以放入原点和目标字段。然而,由于NOT_FOUND"它回应"路线请求失败。 谁能帮助我?谢谢:))
HTML字段
<input type="text" name="departure" id="autocomplete_address">
<input type="button" id="departure_check" value="Add this departure">
<input type="text" name="arrival" id="autocomplete_address2">
<input type="button" id="arrival_check" value="Add this destination" >
使用Javascript:
var latorigine;
var latdestination;
function initMap() {
var geocoder = new google.maps.Geocoder;
var directionsService = new google.maps.DirectionsService;
var directionsDisplay = new google.maps.DirectionsRenderer;
map = new google.maps.Map(document.getElementById('searchmap'), {
center: {lat: 50.437616, lng: 2.809546},
zoom: 15
});
directionsDisplay.setMap(map);
//AUTOCOMPLETE
var departure_input =(document.getElementById('autocomplete_address'));
var arrival_input =(document.getElementById('autocomplete_address2'));
var options = {
types: ['address'],
componentRestrictions: {country: "fr"}
}
var departure_autocomplete = new
google.maps.places.Autocomplete(departure_input,options);
var arrival_autocomplete = new
google.maps.places.Autocomplete(arrival_input,options);
document.getElementById('departure_check').addEventListener('click', function(latorigine) {
var departure_place = departure_autocomplete.getPlace();
latorigine = ' " '+ departure_place.geometry.location.lat() + ", " + departure_place.geometry.location.lng()+ ' " ';
console.log(latorigine);
});
document.getElementById('arrival_check').addEventListener('click', function(latdestination) {
var arrival_place = arrival_autocomplete.getPlace();
latdestination = ' " ' +arrival_place.geometry.location.lat() + ", " + arrival_place.geometry.location.lng()+ ' " ';
console.log(latdestination);
});
}
//END INITMAP
function trajectdirection(directionsService, directionsDisplay,latorigine, latdestination) {
directionsService.route({
origin: latorigine.toString(),
destination: latdestination.toString(),
travelMode: 'DRIVING'
}, function(response, status) {
if (status === 'OK') {
directionsDisplay.setDirections(response);
} else {
window.alert('Directions request failed due to ' + status);
}
}
});
}