我正在做一个学校项目,我正在使用Google地理编码API的json
输出。
我不知道如何存储" formatted_address" (见下文)变量中的输出,以便稍后我可以使用这些信息。
以下是我正在处理的json
输出的一部分。
"results" : [
{
"address_components" : [
{
"long_name" : "11A",
"short_name" : "11A",
"types" : [ "street_number" ]
},
{
"long_name" : "Albyn Place",
"short_name" : "Albyn Pl",
"types" : [ "route" ]
},
{
"long_name" : "Aberdeen",
"short_name" : "Aberdeen",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Aberdeen",
"short_name" : "Aberdeen",
"types" : [ "postal_town" ]
},
{
"long_name" : "Aberdeen City",
"short_name" : "Aberdeen City",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "United Kingdom",
"short_name" : "GB",
"types" : [ "country", "political" ]
},
{
"long_name" : "AB10 1YE",
"short_name" : "AB10 1YE",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "11A Albyn Place, Aberdeen, Aberdeen City AB10 1YE, UK",
"geometry" : {
"location" : {
"lat" : 57.1425663,
"lng" : -2.1144957
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 57.1439152802915,
"lng" : -2.113146719708498
},
"southwest" : {
"lat" : 57.1412173197085,
"lng" : -2.115844680291502
}
}
},
"place_id" : "ChIJ89KpnycOhEgR96EdolCIM8U",
"types" : [ "street_address" ]
},
{
"address_components" : [
{
"long_name" : "Union Grove (o/s 82)",
"short_name" : "Union Grove (o/s 82)",
"types" : [ "bus_station", "transit_station", "establishment" ]
},
{
"long_name" : "Aberdeen",
"short_name" : "Aberdeen",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Aberdeen",
"short_name" : "Aberdeen",
"types" : [ "postal_town" ]
},
{
"long_name" : "Aberdeen City",
"short_name" : "Aberdeen City",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "United Kingdom",
"short_name" : "GB",
"types" : [ "country", "political" ]
},
{
"long_name" : "AB10",
"short_name" : "AB10",
"types" : [ "postal_code_prefix", "postal_code" ]
}
],
"formatted_address" : "Union Grove (o/s 82), Aberdeen, Aberdeen, Aberdeen City AB10, UK",
"geometry" : {
"location" : {
"lat" : 57.141483,
"lng" : -2.115739
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 57.1428319802915,
"lng" : -2.114390019708498
},
"southwest" : {
"lat" : 57.1401340197085,
"lng" : -2.117087980291502
}
}
},
"place_id" : "ChIJra8KiycOhEgRSR-z6DmOvO8",
"types" : [ "bus_station", "transit_station", "establishment" ]
},
{
"address_components" : [
{
"long_name" : "AB10 1YE",
"short_name" : "AB10 1YE",
"types" : [ "postal_code" ]
},
{
"long_name" : "Aberdeen",
"short_name" : "Aberdeen",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Aberdeen",
"short_name" : "Aberdeen",
"types" : [ "postal_town" ]
},
{
"long_name" : "Aberdeen City",
"short_name" : "Aberdeen City",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "United Kingdom",
"short_name" : "GB",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Aberdeen, Aberdeen, Aberdeen City AB10 1YE, UK",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 57.1429049,
"lng" : -2.1140542
},
"southwest" : {
"lat" : 57.141979,
"lng" : -2.1148766
}
},
"location" : {
"lat" : 57.1425663,
"lng" : -2.1144957
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 57.14379093029149,
"lng" : -2.113116419708498
},
"southwest" : {
"lat" : 57.1410929697085,
"lng" : -2.115814380291502
}
}
},
"place_id" : "ChIJWey5nycOhEgRXR9pzwov_pY",
"types" : [ "postal_code" ]
},
{
"address_components" : [
{
"long_name" : "AB10",
"short_name" : "AB10",
"types" : [ "postal_code_prefix", "postal_code" ]
},
{
"long_name" : "Aberdeen",
"short_name" : "Aberdeen",
"types" : [ "postal_town" ]
},
{
"long_name" : "United Kingdom",
"short_name" : "GB",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Aberdeen AB10, UK",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 57.1511959,
"lng" : -2.0903783
},
"southwest" : {
"lat" : 57.1094073,
"lng" : -2.1530504
}
},
"location" : {
"lat" : 57.131255,
"lng" : -2.1264712
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 57.1511959,
"lng" : -2.0903783
},
"southwest" : {
"lat" : 57.1094073,
"lng" : -2.1530504
}
}
},
"place_id" : "ChIJOT6RmCAOhEgRRCBB7_j2M30",
"types" : [ "postal_code_prefix", "postal_code" ]
},
{
"address_components" : [
{
"long_name" : "Aberdeen",
"short_name" : "Aberdeen",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Aberdeen City",
"short_name" : "Aberdeen City",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Scotland",
"short_name" : "Scotland",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United Kingdom",
"short_name" : "GB",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Aberdeen, Aberdeen City, UK",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 57.19565069999999,
"lng" : -2.0461811
},
"southwest" : {
"lat" : 57.1041518,
"lng" : -2.2058926
}
},
"location" : {
"lat" : 57.149717,
"lng" : -2.094278
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 57.19565069999999,
"lng" : -2.0461811
},
"southwest" : {
"lat" : 57.1041518,
"lng" : -2.2058926
}
}
},
"place_id" : "ChIJSXXXH0wFhEgRcsT0XNoFu-g",
"types" : [ "locality", "political" ]
},
{
"address_components" : [
{
"long_name" : "Aberdeen City",
"short_name" : "Aberdeen City",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Scotland",
"short_name" : "Scotland",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United Kingdom",
"short_name" : "GB",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Aberdeen City, UK",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 57.2353521,
"lng" : -2.0461828
},
"southwest" : {
"lat" : 57.07618890000001,
"lng" : -2.3609398
}
},
"location" : {
"lat" : 57.1506564,
"lng" : -2.1467599
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 57.2353521,
"lng" : -2.0461828
},
"southwest" : {
"lat" : 57.07618890000001,
"lng" : -2.3609398
}
}
},
"place_id" : "ChIJs6qVe2kRhEgRkLBCBhpoDAM",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"address_components" : [
{
"long_name" : "Aberdeen",
"short_name" : "Aberdeen",
"types" : [ "postal_town" ]
},
{
"long_name" : "United Kingdom",
"short_name" : "GB",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Aberdeen, UK",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 57.3274181,
"lng" : -2.0076331
},
"southwest" : {
"lat" : 57.0411197,
"lng" : -2.3365801
}
},
"location" : {
"lat" : 57.2177758,
"lng" : -2.1823124
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 57.3274181,
"lng" : -2.0076331
},
"southwest" : {
"lat" : 57.0411197,
"lng" : -2.3365801
}
}
},
"place_id" : "ChIJe57LATIShEgRdUZWMa_i0ag",
"types" : [ "postal_town" ]
},
{
"address_components" : [
{
"long_name" : "Scotland",
"short_name" : "Scotland",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United Kingdom",
"short_name" : "GB",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Scotland, UK",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 60.8607515,
"lng" : -0.7246751
},
"southwest" : {
"lat" : 54.6332381,
"lng" : -8.6498565
}
},
"location" : {
"lat" : 56.49067119999999,
"lng" : -4.2026458
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 60.84567610000001,
"lng" : -0.7247473999999999
},
"southwest" : {
"lat" : 54.6332381,
"lng" : -8.6492466
}
}
},
"place_id" : "ChIJn6HyA8TiYUgRFAfDCdj6wec",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"address_components" : [
{
"long_name" : "United Kingdom",
"short_name" : "GB",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "United Kingdom",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 60.8606697,
"lng" : 33.916555
},
"southwest" : {
"lat" : 34.5626034,
"lng" : -8.649357199999999
}
},
"location" : {
"lat" : 55.378051,
"lng" : -3.435973
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 60.856553,
"lng" : 1.7627096
},
"southwest" : {
"lat" : 49.8669688,
"lng" : -8.649357199999999
}
}
},
"place_id" : "ChIJqZHHQhE7WgIReiWIMkOg-MQ",
"types" : [ "country", "political" ]
}
],
"status" : "OK"
到目前为止,我在我的js文件中尝试这个:
var restourantAddressRequest = "https://maps.googleapis.com/maps/api/geocode/json?latlng="+restLatitude+","+restLongtitude+"&key=API_KEY;
//alert(restourantAddressRequest.results.formatted_address);
$("#restaurant_address").html("Address: " + restourantAddressRequest.results.formatted_address);
答案 0 :(得分:2)
您想使用点表示法来获得所需的值。 Google V3 Geocoder会返回一个数组,因此您需要查找以下内容:
results[0].formatted_address
这是一个小提琴,可以为您提醒格式化地址:
http://jsfiddle.net/iamjpg/h39041w2/
我希望这有用。
答案 1 :(得分:1)
在访问JSON响应之前,您需要解析它:
var restourantAddressRequest = "https://maps.googleapis.com/maps/api/geocode/json?latlng="+restLatitude+","+restLongtitude+"&key=API_KEY";
var json= JSON.parse(restourantAddressRequest);
//alert(restourantAddressRequest.results.formatted_address);
$("#restaurant_address").html("Address: " + restourantAddressRequest.results.formatted_address);
希望这会奏效。
答案 2 :(得分:1)
如果您想异步执行此操作:
//Show that you are loading the data
$("#restaurant_address").html("Address: Loading... ");
//Set the url:
var url = 'https://maps.googleapis.com/maps/api/geocode/json?latlng="+restLatitude+","+restLongtitude+"&key=API_KEY"';
//Make the Ajax call:
$.ajax({
type: 'GET',
async: true,
url: url,
dataType: "json",
success: function(responseObject){
if (responseObject.success)
{
$("#restaurant_address").html("Address: " + ... );
} else {
$("#restaurant_address").html("Address: n/a");
}
}
});
答案 3 :(得分:0)
我在jQuery中使用getJSON
方法解决了这个问题:
var restaurantAddressRequest = "https://maps.googleapis.com/maps/api/geocode/json?latlng="+restLatitude+","+restLongtitude+"&key=API_KEY";
//alert(restourantAddressRequest.results.formatted_address);
$.getJSON(restaurantAddressRequest,function(jsondata){
$("#restaurant_address").html("Address: " + jsondata.results[0].jsondata.results[0].formatted_address);
});