gpt4 book ai didi

javascript - 使用路线服务时 GUnload 为空或未定义

转载 作者:行者123 更新时间:2023-11-28 09:40:30 25 4
gpt4 key购买 nike

我在使用 Google Maps API V3 时遇到了一个我无法理解的错误。我的初始 map 显示得很好,但是当我尝试获取路线时,出现以下两个错误:

错误:属性“GUnload”的值为 null 或未定义,而不是 Function 对象

错误:无法获取属性“setDirections”的值:对象为 null 或未定义

我没有在任何地方使用 GUnload,所以我不明白为什么会收到该错误。就第二个错误而言,就好像路线服务出了问题。

这是我的代码:

var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
function initialize(address) {

directionsDisplay = new google.maps.DirectionsRenderer();
var geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(42.733963, -84.565501);
var mapOptions = {
center: latlng,
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
directionsDisplay.setMap(map);
}

function getDirections(start, end) {
var request = {
origin:start,
destination:end,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(result);
} else {
alert("Directions cannot be displayed for the following reason: " + status);
}
});
}

我对 javascript 不太了解,所以我可能会犯一些错误。我很感激我能得到的任何帮助。

最佳答案

GUnload 是 Google Maps API v2 的东西。如果您收到一条错误,指出它未定义,则意味着您的某些代码(或您的应用程序中包含的代码)正在使用它,并且您正在使用 Google Maps API v3,但它不提供它.

在没有看到更多上下文的情况下无法评论 setDirections 问题,但很可能是由于在加载 API 代码之前尝试调用它。

关于javascript - 使用路线服务时 GUnload 为空或未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12461051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com