【发布时间】: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