简体   繁体   English

Google Maps API v3 验证地点

[英]Google Maps API v3 validate place

Is it possible to validate if place exist?是否可以验证地方是否存在? In my example below when you fill something like "wkjdbvkwe" as destination place, it does nothing.. I want to show some error message and don't know how to validate if destination =! real place在我下面的示例中,当您将"wkjdbvkwe"类的内容填充为目标位置时,它什么也不做。我想显示一些错误消息,但不知道如何验证if destination =! real place if destination =! real place . 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>

Thank you!谢谢!

Add an else to the if (status == google.maps.DirectionsStatus.OK)将 else 添加到if (status == google.maps.DirectionsStatus.OK)

 if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
 } else {
    alert("Can't create directions:" + status);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM