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

标签 javascript google-maps-api-3

我在使用 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/

相关文章:

google-maps - 使用 svg 标记时缩放期间的 Google map 标记对齐问题

javascript - JSON - 输入第二级对象

javascript - 具有各种高度的 jquery 可排序 block

javascript - document read 和 console.log() 中的 offset.top 值不同,为什么?

javascript - 单击集群时,如何显示具有标记值的信息窗口?

google-maps - 我可以使用我自己的自定义 gif 动画作为 Google Maps API 上的标记吗?

javascript - 触发按钮点击其他元素

JavaScript 排序

javascript - 使用登录的 Google map 绘制标记和保存标记时出现问题

javascript - 如何关闭谷歌地图中的 map ?