【问题标题】:Getting an error while loading map: Uncaught TypeError: undefined is not a function加载地图时出错:Uncaught TypeError: undefined is not a function
【发布时间】:2014-04-23 06:39:19
【问题描述】:

我正在尝试使用 Google 地图 API。我在其中使用 Google DirectionsService 在地图上绘制路线。

我已经存储了几个 DirectionResults 对象。我想加载到地图上。
下面是我写的代码;

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>

加载存储的 json(DirectionsResult 对象格式)

$.getJSON('GoogleJson.json', function (json) {
    debugger;
    data = json;
});

调用DirectionsService.Route()方法;

directionsService.route(request, function (response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(data);
        console.log(data);
        console.log(response);
    }
});

这是一个错误。如果我们在setDirections() 中使用响应对象,它会渲染带有路线的地图。但是,如果我使用 getJSON() 方法从文件中加载它,则会出现上述错误。

在上面的代码中,如果我们查看数据和响应对象,两者都是一样的。

编辑:
我将支票添加为

if (data != response) {
    console.log(typeof data);
    console.log(typeof response);
}

if (data == response)
    alert('both objects are same'); 

对象似乎不一样。 typeof 声明说两者都是对象类型。

【问题讨论】:

  • 显然,data !== response。你可以为我们console.log(typeof data); console.log(typeof response);吗?
  • 我根据您的建议添加了检查 if (data != response) { console.log(typeof data); console.log(响应类型); } if (data == response) alert('两个对象相同');对象似乎不一样。 typeof 语句表示两者都是对象类型。
  • 嗯,使用this function,toType(data) 和toType(response) 返回什么?更好的是,这两个变量的内容是什么?
  • 如果我们看到这两个对象的内容,它会为我们提供类似于 DirectionsResult 对象的格式,如 routes[].legs[].steps[] 集合。
  • 您的对象data 必须是google.maps.DirectionsResult 对象。好像不是。

标签: javascript jquery google-maps-api-3


【解决方案1】:

假设您的数据完全包含 routes 所期望的 directionsDisplay.setDirections(我不这么认为):在所需的方向结果中还有另一个未记录的属性(返回原始请求的函数)。这个函数的名字是动态的,所以它可能:

  1. 不存储在 JSON 中(因为它是一个函数)
  2. 不能以任何方式安全恢复(因为您不知道函数的预期名称)

结论:存储的方向结果可能无法创建方向(至少跨会话不安全)

我能想到的唯一解决方法是也存储原始请求,发送一个虚拟请求以检索函数所需的名称,然后使用该名称恢复函数。

【讨论】:

  • 还有其他方法吗?我不想使用谷歌服务数据,而是由某人提供的数据,在这种情况下是管理员。
  • 不清楚。响应是原始结果,其中数据存储结果。我们应该恢复哪个功能以及如何恢复?
  • 对不起,我以为是函数(也许它已经改变了),现在它是一个包含请求的普通对象(第一个参数通常传递给route())。目前对我来说这个对象的名称是 Tb 。它必须至少包含 travelMode 。
  • yes tb 存在于两个对象中,在各自的属性中具有相同的值。
  • 你能发布json吗?
猜你喜欢
  • 1970-01-01
  • 2014-08-15
  • 2015-01-14
  • 2014-07-06
  • 2014-09-01
  • 2015-11-04
  • 2013-03-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多