方向 API google - 方向请求因未找到而失败
Posted
技术标签:
【中文标题】方向 API google - 方向请求因未找到而失败【英文标题】:Direction API google - direction request failed due to not found 【发布时间】:2016-11-05 18:42:21 【问题描述】:我试图显示两点之间的距离。但我正面临此错误方向请求因未找到而失败。我用谷歌搜索但没有找到解决方案。请看看我的代码,如果我做错了什么,请告诉我。
function initAutocomplete()
var markerArray = [];
// Instantiate a directions service.
var directionsService = new google.maps.DirectionsService;
// Create a map and center it on Manhattan.
var map = new google.maps.Map(document.getElementById('map-canvas'),
zoom: 13,
center: lat: 40.771, lng: -73.974
);
// Create a renderer for directions and bind it to the map.
var directionsDisplay = new google.maps.DirectionsRenderer(map: map);
// Instantiate an info window to hold step text.
var stepDisplay = new google.maps.InfoWindow;
// Display the route between the initial start and end selections.
calculateAndDisplayRoute(
directionsDisplay, directionsService, markerArray, stepDisplay, map);
// Listen to change events from the start and end lists.
var onChangeHandler = function()
calculateAndDisplayRoute(
directionsDisplay, directionsService, markerArray, stepDisplay, map);
;
document.getElementById('start').addEventListener('change', onChangeHandler);
document.getElementById('end').addEventListener('change', onChangeHandler);
function calculateAndDisplayRoute(directionsDisplay, directionsService,
markerArray, stepDisplay, map)
// First, remove any existing markers from the map.
for (var i = 0; i < markerArray.length; i++)
markerArray[i].setMap(null);
// Retrieve the start and end locations and create a DirectionsRequest using
// WALKING directions.
directionsService.route(
origin: document.getElementById('start').value,
destination: document.getElementById('end').value,
travelMode: google.maps.TravelMode.WALKING
, function(response, status)
// Route the directions and pass the response to a function to create
// markers for each step.
if (status === google.maps.DirectionsStatus.OK)
document.getElementById('warnings-panel').innerhtml =
'<b>' + response.routes[0].warnings + '</b>';
directionsDisplay.setDirections(response);
showSteps(response, markerArray, stepDisplay, map);
else
window.alert('Directions request failed due to ' + status);
);
function showSteps(directionResult, markerArray, stepDisplay, map)
// For each step, place a marker, and add the text to the marker's infowindow.
// Also attach the marker to an array so we can keep track of it and remove it
// when calculating new routes.
var myRoute = directionResult.routes[0].legs[0];
for (var i = 0; i < myRoute.steps.length; i++)
var marker = markerArray[i] = markerArray[i] || new google.maps.Marker;
marker.setMap(map);
marker.setPosition(myRoute.steps[i].start_location);
attachInstructionText(
stepDisplay, marker, myRoute.steps[i].instructions, map);
function attachInstructionText(stepDisplay, marker, text, map)
google.maps.event.addListener(marker, 'click', function()
// Open an info window when the marker is clicked on, containing the text
// of the step.
stepDisplay.setContent(text);
stepDisplay.open(map, marker);
);
HTML 端:
<input id="start" class="controls" type="text"
placeholder="picup lock">
<input id="end" class="controls" type="text"
placeholder="drop off">
【问题讨论】:
收到ZERO_RESULTS
时,您使用的开始和结束值是什么?请提供一个 Minimal, Complete, Tested and Readable example 来证明错误。
你真的在使用已弃用并关闭的google-maps-api-2吗?
不,它没有关闭并且工作正常
在我看来你实际上是在使用google-maps-api-3,如果是这样,你应该修复你问题上的标签。
【参考方案1】:
根据此documentation,错误 NOT_FOUND 表明请求的起点、目的地或航路点中指定的至少一个位置无法进行地理编码。
确保 point1 和 point3 不为空。 API 不知道如何创建从“”到“”的路由。
检查这个相关的ticket和tutorial。
【讨论】:
【参考方案2】:我昨天遇到了同样的问题,通过 C# httpClient 使用 API。但同样的请求在浏览器中运行良好。经过一番调试,我发现只有在 httpClient 中发送 Accept-Language 标头才有效……
【讨论】:
以上是关于方向 API google - 方向请求因未找到而失败的主要内容,如果未能解决你的问题,请参考以下文章
Google Maps JavaScript API v3 方向功能
在 Google-maps-v3 方向 API 中传递 LatLng 值