因此,我尝试从Google Directions API获取转机路线,将步骤存储在数组中,以便查看正在使用的转接线路。我知道该服务正在接受我的API密钥,因为它成功创建了LatLng对象,如果我的脚本标记不正确,则不会发生这种情况。
但是在调试中我确定route()函数没有执行,或者回调函数无法执行。我将在调试结果中发表评论:
startPnt = new google.maps.LatLng(32.741480, -96.853803);
endPnt = new google.maps.LatLng(32.921941, -96.735327);
var request = {
origin: startPnt,
destination: endPnt,
travelMode: google.maps.TravelMode.TRANSIT,
provideRouteAlternatives: false //*typo spotted here, was formerly provideRoutesAlternatives.*
};
window.alert(request.origin.lat()); //executed successfully
var stepResults = new Array();
window.alert(stepResults.length); //executed successfully
var dService = new google.maps.DirectionsService();
dService.route(request, function(results, status){
window.alert("The function inside dService.route() is running"); /*did not execute*/
if(status==google.maps.DirectionsStatus.OK) { window.alert("Success"); /*did not execute */ stepResults = results.routes[0].legs[0].steps; /*in original code, this DOM/JSON typo was preventing the callback function from running. The typo had left out "results"*/ }
if(status==google.maps.DirectionsStatus.INVALID_REQUEST) { window.alert("Invalid request."); }
if(status==google.maps.DirectionsStatus.OVER_QUERY_LIMIT) { window.alert("Google has stopped accepting queries from this key for today.");//did not execute }
if(status==google.maps.DirectionsStatus.UNKNOWN_ERROR) { window.alert("Google servers encountered an unexpected error. Try again momentarily."); //did not execute }
else window.alert("Not success."); //did not execute
}
);
这段代码确实运行了一次,但我在调整数据采集后再次运行它并且route()已经退出了工作。我无法弄清楚为什么dService.route()不起作用,或者为什么回调函数拒绝做任何事情。不过,我已经检查了API控制台,Google还没有收到任何使用我的API密钥的请求。这里的问题是什么 - 可能是我的内部网阻止了请求吗?
答案 0 :(得分:0)
您有语法错误。这个fiddle适用于我
var startPnt = new google.maps.LatLng(32.741480, -96.853803);
var endPnt = new google.maps.LatLng(32.921941, -96.735327);
var request = {
origin: startPnt,
destination: endPnt,
travelMode: google.maps.TravelMode.TRANSIT,
provideRouteAlternatives: false //*typo spotted here, was formerly provideRoutesAlternatives.*
};
window.alert(request.origin.lat()); //executed successfully
var stepResults = new Array();
window.alert(stepResults.length); //executed successfully
var dService = new google.maps.DirectionsService();
dService.route(request, function (results, status) {
window.alert("The function inside dService.route() is running"); /*did not execute*/
if (status == google.maps.DirectionsStatus.OK) {
window.alert("Success"); /*did not execute */
stepResults = results.routes[0].legs[0].steps; /*in original code, this DOM/JSON typo was preventing the callback function from running. The typo had left out "results"*/
} else if (status == google.maps.DirectionsStatus.INVALID_REQUEST) {
window.alert("Invalid request.");
} else if (status == google.maps.DirectionsStatus.OVER_QUERY_LIMIT) {
window.alert("Google has stopped accepting queries from this key for today."); //did not execute
} else if (status == google.maps.DirectionsStatus.UNKNOWN_ERROR) {
window.alert("Google servers encountered an unexpected error. Try again momentarily."); //did not execute
} else window.alert("Not success."); //did not execute
});