【问题标题】:javascript fails to execute mapquest apijavascript 无法执行 mapquest api
【发布时间】:2011-11-17 21:44:08
【问题描述】:

我想将此代码发送给alert 用户及其透视地址,但由于某种原因它没有执行。有人能指出我正确的方向吗?

谢谢!

<html>
<head>
<title>geoload</title>
<script type="text/javascript" src="http://www.mapquestapi.com/geocoding/v1/reverse?key=MY_DEV KEY GOES HERE=40.0755&lng=-76.329999&output=json&callback=renderGeocode"></script>

<script type="text/javascript">
function renderGeocode(response){
    var location = response.results[0].locations[0];
    alert(location.adminArea5 + ", " + location.adminArea4);
}
</script>
</head>
<body onload=(load"renderGeocode")></body>
</html>

【问题讨论】:

  • location.adminArea5 我认为您缺少一个“+”
  • 你在哪里调用这个函数?
  • 我看到了回调,但你在哪里传递任何东西给它?无论如何,我都不是 javascript 专家,但在我看来,您正在调用一个不存在的函数。

标签: javascript json api alert mapquest


【解决方案1】:

您错过了+ 标志:

alert(location.adminArea5 + ", " + location.adminArea4);
                          ^

【讨论】:

  • 感谢您的回复。我添加了 + 号,还添加了 onload="load(renderGeocode)",但仍然没有运气。
【解决方案2】:

body onload 不是必需的,因为对 mapquestapi 的调用包含一个回调参数。回调参数是renderGeocode,它将调用同名的javascript函数。见以下代码:

<html>
<head>
<title>geoload</title>
<script type="text/javascript" >
function renderGeocode(response) {
    console.log(response)
} 

</script>
<script type="text/javascript" src="http://www.mapquestapi.com/geocoding/v1/reverse?key=MY_DEV KEY GOES HERE&lat=40.0755&lng=-76.329999&callback=renderGeocode" ></script>
</head>
<body></body>
</html>

【讨论】:

  • 您应该在回答中加入文字说明。不仅仅是带有简短注释的代码示例
猜你喜欢
  • 2012-11-08
  • 2013-11-08
  • 2014-04-21
  • 2017-12-16
  • 2019-07-31
  • 2016-04-24
  • 2012-03-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多