我希望在两个点之间创建驾驶方向,其中一个或两个点没有直接通往它的道路,但靠近它。
例如,如果您使用DirectionsService尝试创建一条之间的摩押,犹他州和锡安国家公园,锡安国家公园,UT你将返回Zero_Results,因为没有道路到中心(最后,谷歌返回的液化天然气)锡安国家公园。如果你在google.com/map上做同样的事情,你会看到一条从摩押到锡安国家公园东门的车道线,并步行到公园的中心(大头针所在的地方)。他们是怎么决定去锡安国家公园的呢?
发布于 2016-03-29 03:07:37
如果您reverse geocode锡安国家公园的地理编码器返回的坐标(37.2982022,-113.0263005),第一个结果将是道路上最近的位置。
proof of concept fiddle
代码片段:
var geocoder;
var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer();
function initialize() {
var map = new google.maps.Map(
document.getElementById("map_canvas"), {
center: new google.maps.LatLng(37.4419, -122.1419),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var start = "Moab, UT";
directionsDisplay.setMap(map);
geocodeAddress("Zion National Park", start);
}
function geocodeAddress(address, start) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
'address': address
}, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
geocoder.geocode({
'location': results[0].geometry.location
}, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
calculateAndDisplayRoute(start, results[0].geometry.location)
} else {
window.alert('Reverse Geocode failed due to: ' + status);
}
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
function calculateAndDisplayRoute(start, end) {
directionsService.route({
origin: start,
destination: end,
travelMode: google.maps.TravelMode.DRIVING
}, function(response, status) {
if (status === google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
} else {
window.alert('Directions request failed due to ' + status);
}
});
}
google.maps.event.addDomListener(window, "load", initialize);html,
body,
#map_canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas"></div>
https://stackoverflow.com/questions/36267314
复制相似问题