是否可以验证地点是否存在?在下面的示例中,当您将"wkjdbvkwe"
填充为目标位置时,它什么也没做。我想显示一些错误消息,并且不知道如何验证if destination =! real place
。
var source, destination;
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
google.maps.event.addDomListener(window, 'load', function () {
new google.maps.places.SearchBox(document.getElementById('txtDestination'));
directionsDisplay = new google.maps.DirectionsRenderer({ 'draggable': true });
});
function GetRoute() {
//*********DIRECTIONS**********************//
source = "Čadca, Slovensko";
destination = document.getElementById("txtDestination").value;
var request = {
origin: source,
destination: destination,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function (response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
//*********DISTANCE AND DURATION**********************//
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix({
origins: [source],
destinations: [destination],
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC,
avoidHighways: false,
avoidTolls: false
}, function (response, status) {
if (status == google.maps.DistanceMatrixStatus.OK && response.rows[0].elements[0].status != "ZERO_RESULTS") {
var distance = response.rows[0].elements[0].distance.text;
var dvDistance = document.getElementById("distance");
dvDistance.innerHTML = "";
dvDistance.innerHTML += distance;
} else {
alert("Nie je možné vypočítať vzdialenosť");
}
});
}

<script src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places.js"></script>
Destination: <input type="text" id="txtDestination" onblur="GetRoute()" />
<br/>
<span id="distance"></span>
&#13;
谢谢!
答案 0 :(得分:1)
在if (status == google.maps.DirectionsStatus.OK)
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
} else {
alert("Can't create directions:" + status);
}