我正在使用Google Map Direction API并尝试为以下地址绘制路线。
以下对上述地址不起作用。请帮忙。
<script>
function initMap() {
var directionsService = new google.maps.DirectionsService;
var directionsDisplay = new google.maps.DirectionsRenderer;
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 6,
center: {lat: 41.85, lng: -87.65}
});
directionsDisplay.setMap(map);
document.getElementById('submit').addEventListener('click', function() {
calculateAndDisplayRoute(directionsService, directionsDisplay);
});
}
function calculateAndDisplayRoute(directionsService, directionsDisplay) {
var waypts = [];
var checkboxArray = document.getElementById('waypoints');
for (var i = 0; i < checkboxArray.length; i++) {
if (checkboxArray.options[i].selected) {
waypts.push({
location: checkboxArray[i].value,
stopover: true
});
}
}
directionsService.route({
origin: document.getElementById('start').value,
destination: document.getElementById('end').value,
waypoints: waypts,
optimizeWaypoints: true,
travelMode: 'DRIVING'
}, function(response, status) {
if (status === 'OK') {
debugger;
directionsDisplay.setDirections(response);
} else {
window.alert('Directions request failed due to ' + status);
}
});
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=YOURKEY&callback=initMap">
</script>
答案 0 :(得分:1)
那些&#34;地方&#34;不是地址。使用Places API获取placeId,或使用他们的邮政地址。
请参阅Geocoding Best Practices in the documentation
使用案例
用户输入的模糊查询(例如,地址不完整或格式不正确)
将API放置自动填充服务以获取地点ID,然后使用地理编码API将地点ID地理编码为latlng。
proof of concept fiddle(在Google上查看地点{&#39; s PlaceId Finder)
代码段
Not.Nullable()
&#13;
html,
body,
#map {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
&#13;