我想将administrative_area_level_1
值检索为状态,将locality
值作为区域检索,将formatted_address
值作为地址检索以供进一步使用。我使用谷歌地图api,我给出纬度和经度,它将返回包含我想要的数据的json对象。
下面是我用来检索数据的网址示例,其中包含我想要的结果部分。
http://maps.google.com/maps/api/geocode/json?latlng=1.56406166666667,103.626493333333&sensor=true
{
"results" : [
{
"address_components" : [
{
"long_name" : "Jalan Cengal",
"short_name" : "Jalan Cengal",
"types" : [ "route" ]
},
{
"long_name" : "Johor Bahru",
"short_name" : "Johor Bahru",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Johor",
"short_name" : "Johor",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Malaysia",
"short_name" : "MY",
"types" : [ "country", "political" ]
},
{
"long_name" : "80990",
"short_name" : "80990",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Jalan Cengal, 80990 Johor Bahru, Johor, Malaysia",
......
首先,这是我编码以检索我想要的数据。
private void extractPosition(JSONObject json)
{
JSONObject result = json.optJSONArray("results").optJSONObject(0);
JSONArray address_components = result.optJSONArray("address_components");
area.setText(address_components.optJSONObject(1).optString("short_name"));
state.setText(address_components.optJSONObject(2).optString("short_name"));
address.setText(result.optString("formatted_address"));
}
但后来我意识到位置并不总是相同,因为有些坐标有下面的附加信息,所以我不能依赖数据的位置。
{
"results" : [
{
"address_components" : [
{
"long_name" : "Jalan Kebudayaan 11",
"short_name" : "Jalan Kebudayaan 11",
"types" : [ "route" ]
},
{
"long_name" : "Taman Universiti",
"short_name" : "Taman Universiti",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "Skudai",
"short_name" : "Skudai",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Johor",
"short_name" : "Johor",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Malaysia",
"short_name" : "MY",
"types" : [ "country", "political" ]
},
{
"long_name" : "81300",
"short_name" : "81300",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Jalan Kebudayaan 11, Taman Universiti, 81300 Skudai, Johor, Malaysia",
.....
我应该如何修改我的编码,以便检索administrative_area_level_1
和locality
值?
答案 0 :(得分:0)
private void extractPosition(JSONObject json)
{
JSONObject result = json.optJSONArray("results").optJSONObject(0);
JSONArray address_components = result.optJSONArray("address_components");
for (int i =0; i<address_components.lenght;i++){
if(address_components.optJSONObject(1).optString("short_name").equals("locality"))
area.setText(address_components.optJSONObject(1).optString("short_name"));
if (address_components.optJSONObject(1).optString("short_name").equals("administrative_area_level_1"))
state.setText(address_components.optJSONObject(2).optString("short_name"));
address.setText(result.optString("formatted_address"));
}
}
答案 1 :(得分:0)
private void extractPosition(JSONObject json)
{
JSONObject result = json.optJSONArray("results").optJSONObject(0);
JSONArray address_components = result.optJSONArray("address_components");
for (int i =0; i<address_components.length();i++)
{
String searchKeyword;
searchKeyword = address_components.optJSONObject(i).optString("types");
Log.d("test", searchKeyword);
if (searchKeyword.contains("locality"))
{
area.setText(address_components.optJSONObject(i).optString("short_name"));
}
if (searchKeyword.contains("administrative_area_level_1"))
{
state.setText(address_components.optJSONObject(i).optString("short_name"));
}
}
address.setText(result.optString("formatted_address"));
}