嗨,我的代码附在下面,尽管它是更大代码的一部分
<html>
<head>
<style>
#results{margin-top:100px; width:600px; border:1px solid #000000; background-color:#CCCCCC; min-height:200px;}
</style>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
</head>
<body>
<div id="results"></div>
<script>
var input="Taj Mahal India";
var url = "http://maps.googleapis.com/maps/api/geocode/json?address=" + input + "&sensor=false";
var check = $.getJSON(url, function (data) {
console.log("inside getjson");
console.log(data);
$.each(data.results.address_components, function(i,inside){
if(inside.types[0]=="route"){
$("#results").append('<p> route : '+this.long_name +'</p>');
}
if(inside.types[0]=="locality"){
$("#results").append('<p> locality : '+this.long_name +'</p>');
}
if(inside.types[0]=="administrative_area_level_2"){
$("#results").append('<p> administrative area 2 : '+this.long_name +'</p>');
}
if(inside.types[0]=="administrative_area_level_1"){
$("#results").append('<p> administrative area 1 : '+this.long_name +'</p>');
}
if(inside.types[0]=="country"){
$("#results").append('<p> country : '+this.long_name +'</p>');
}
});
});
</script>
</body>
</html>
问题是它不起作用。我非常怀疑我做错了什么$.each(data.results.address_components, function(i,inside)
。
泰姬陵印度的 JSON 在这里http://maps.googleapis.com/maps/api/geocode/json?address=Taj%20Mahal%20India&sensor=false