javascript无法执行mapquest api

时间:2011-11-17 21:44:09

标签: javascript json api alert mapquest

我希望这个代码能够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>

2 个答案:

答案 0 :(得分:3)

您错过了+标志:

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

答案 1 :(得分:1)

不需要body onload,因为对mapquestapi的调用包含一个回调参数。 callback参数是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>