的財產我有Ajax請求如下的谷歌地圖API,如何訪問對象
$.ajax({
type: "POST",
async: false,
dataType: "json",
url: 'https://maps.googleapis.com/maps/api/geocode/json?key=AIzaSyDWPSMsE_CuOAk5iKJ48ReJQYRe9NUn_mo&address='35010'&sensor=false',
success: function (response) {
console.log(response);
var latlong = JSON.stringify(response.results[0].geometry.filter(function(obj){
return obj.types.indexOf("location") != -1
})[0].lat, null, 4);
latlong = latlong.replace(/['"]+/g, '');
alert(latlong);
}
});
而且我得到了以下JSON響應,
{
"results" : [
{
"address_components" : [
{
"long_name" : "35010",
"short_name" : "35010",
"types" : [ "postal_code" ]
},
{
"long_name" : "Alexander City",
"short_name" : "Alexander City",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Alabama",
"short_name" : "AL",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Alexander City, AL 35010, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 33.1044549,
"lng" : -85.76817199999999
},
"southwest" : {
"lat" : 32.703111,
"lng" : -86.050735
}
},
"location" : {
"lat" : 32.94464350000001,
"lng" : -85.91000889999999
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 33.1044549,
"lng" : -85.76817199999999
},
"southwest" : {
"lat" : 32.703111,
"lng" : -86.050735
}
}
},
"place_id" : "ChIJgSRw98ETjIgROEXAlUkHoR8",
"postcode_localities" : [ "ALEX CITY", "Alexander City" ],
"types" : [ "postal_code" ]
}
],
"status" : "OK"
}
我想LAT長值(在「location
」字段中)。但是,當我運行代碼我得到了「jquery.min.js:4 Uncaught TypeError: response.results[0].geometry.filter is not a function"
請幫助。我是新來的JavaScript。
請有什麼建議嗎? –
等待我試圖解決你的問題 – mean
'filter'是一個數組上的方法,'response.results [0] .geometry'是一個普通對象,而不是數組。 – Quentin