Android:我想以 json 格式从 google map api 中提取地址
Posted
技术标签:
【中文标题】Android:我想以 json 格式从 google map api 中提取地址【英文标题】:Android: I want to extract the Address from google map api in json format 【发布时间】:2013-09-28 05:51:19 【问题描述】:在我的 android 应用程序中:我想像使用地理编码器一样提取位置、行政区域、国家名称、邮政编码、地址行,但我无法从 google map api JSON 格式中提取特定值,因为有时格式不同.
有人有想法吗?..
我的 JSON 飞行
"results" : [
"address_components" : [
"long_name" : "17/T116",
"short_name" : "17/T116",
"types" : []
,
"long_name" : "Thirunagar",
"short_name" : "Thirunagar",
"types" : [ "neighborhood", "political" ]
,
"long_name" : "Jafferkhanpet",
"short_name" : "Jafferkhanpet",
"types" : [ "sublocality", "political" ]
,
"long_name" : "Chennai",
"short_name" : "MAS",
"types" : [ "locality", "political" ]
,
"long_name" : "Chennai",
"short_name" : "Chennai",
"types" : [ "administrative_area_level_2", "political" ]
,
"long_name" : "Tamil Nadu",
"short_name" : "TN",
"types" : [ "administrative_area_level_1", "political" ]
,
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
,
"long_name" : "600083",
"short_name" : "600083",
"types" : [ "postal_code" ]
],
"formatted_address" : "17/T116, Thirunagar, Jafferkhanpet, Chennai, Tamil Nadu 600083, India",
"geometry" :
"location" :
"lat" : 13.028205,
"lng" : 80.20512149999999
,
"location_type" : "ROOFTOP",
"viewport" :
"northeast" :
"lat" : 13.0295539802915,
"lng" : 80.2064704802915
,
"southwest" :
"lat" : 13.0268560197085,
"lng" : 80.20377251970849
,
"types" : [ "street_address" ]
,
"address_components" : [
"long_name" : "Kalaimagal Nagar",
"short_name" : "Kalaimagal Nagar",
"types" : [ "neighborhood", "political" ]
,
"long_name" : "Ekkatuthangal",
"short_name" : "Ekkatuthangal",
"types" : [ "sublocality", "political" ]
,
"long_name" : "Chennai",
"short_name" : "MAS",
"types" : [ "locality", "political" ]
,
"long_name" : "Chennai",
"short_name" : "Chennai",
"types" : [ "administrative_area_level_2", "political" ]
,
"long_name" : "Tamil Nadu",
"short_name" : "TN",
"types" : [ "administrative_area_level_1", "political" ]
,
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
],
"formatted_address" : "Kalaimagal Nagar, Ekkatuthangal, Chennai, Tamil Nadu, India",
"geometry" :
"bounds" :
"northeast" :
"lat" : 13.0279001,
"lng" : 80.207994
,
"southwest" :
"lat" : 13.0235429,
"lng" : 80.20417399999999
,
"location" :
"lat" : 13.0263166,
"lng" : 80.20633549999999
,
"location_type" : "APPROXIMATE",
"viewport" :
"northeast" :
"lat" : 13.0279001,
"lng" : 80.207994
,
"southwest" :
"lat" : 13.0235429,
"lng" : 80.20417399999999
,
"types" : [ "neighborhood", "political" ]
,
"address_components" : [
"long_name" : "Ekkatuthangal",
"short_name" : "Ekkatuthangal",
"types" : [ "sublocality", "political" ]
,
"long_name" : "Chennai",
"short_name" : "MAS",
"types" : [ "locality", "political" ]
,
"long_name" : "Chennai",
"short_name" : "Chennai",
"types" : [ "administrative_area_level_2", "political" ]
,
"long_name" : "Tamil Nadu",
"short_name" : "TN",
"types" : [ "administrative_area_level_1", "political" ]
,
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
],
"formatted_address" : "Ekkatuthangal, Chennai, Tamil Nadu, India",
"geometry" :
"bounds" :
"northeast" :
"lat" : 13.029174,
"lng" : 80.207994
,
"southwest" :
"lat" : 13.013609,
"lng" : 80.18614409999999
,
"location" :
"lat" : 13.020991,
"lng" : 80.19727019999999
,
"location_type" : "APPROXIMATE",
"viewport" :
"northeast" :
"lat" : 13.029174,
"lng" : 80.207994
,
"southwest" :
"lat" : 13.013609,
"lng" : 80.18614409999999
,
"types" : [ "sublocality", "political" ]
,
"address_components" : [
"long_name" : "600083",
"short_name" : "600083",
"types" : [ "postal_code" ]
,
"long_name" : "Chennai",
"short_name" : "MAS",
"types" : [ "locality", "political" ]
,
"long_name" : "Chennai",
"short_name" : "Chennai",
"types" : [ "administrative_area_level_2", "political" ]
,
"long_name" : "Tamil Nadu",
"short_name" : "TN",
"types" : [ "administrative_area_level_1", "political" ]
,
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
],
"formatted_address" : "Chennai, Tamil Nadu 600083, India",
"geometry" :
"bounds" :
"northeast" :
"lat" : 13.046442,
"lng" : 80.21846479999999
,
"southwest" :
"lat" : 13.0235679,
"lng" : 80.1837482
,
"location" :
"lat" : 13.0368076,
"lng" : 80.20145429999999
,
"location_type" : "APPROXIMATE",
"viewport" :
"northeast" :
"lat" : 13.046442,
"lng" : 80.21846479999999
,
"southwest" :
"lat" : 13.0235679,
"lng" : 80.1837482
,
"types" : [ "postal_code" ]
,
"address_components" : [
"long_name" : "Chennai",
"short_name" : "Chennai",
"types" : [ "administrative_area_level_2", "political" ]
,
"long_name" : "Tamil Nadu",
"short_name" : "TN",
"types" : [ "administrative_area_level_1", "political" ]
,
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
],
"formatted_address" : "Chennai, Tamil Nadu, India",
"geometry" :
"bounds" :
"northeast" :
"lat" : 13.1595488,
"lng" : 80.30683239999999
,
"southwest" :
"lat" : 12.9666707,
"lng" : 80.1801967
,
"location" :
"lat" : 13.060422,
"lng" : 80.249583
,
"location_type" : "APPROXIMATE",
"viewport" :
"northeast" :
"lat" : 13.1595488,
"lng" : 80.30683239999999
,
"southwest" :
"lat" : 12.9666707,
"lng" : 80.1801967
,
"types" : [ "administrative_area_level_2", "political" ]
,
"address_components" : [
"long_name" : "Chennai",
"short_name" : "MAS",
"types" : [ "locality", "political" ]
,
"long_name" : "Chennai",
"short_name" : "Chennai",
"types" : [ "administrative_area_level_2", "political" ]
,
"long_name" : "Tamil Nadu",
"short_name" : "TN",
"types" : [ "administrative_area_level_1", "political" ]
,
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
],
"formatted_address" : "Chennai, Tamil Nadu, India",
"geometry" :
"bounds" :
"northeast" :
"lat" : 13.2339843,
"lng" : 80.3322912
,
"southwest" :
"lat" : 12.8339608,
"lng" : 80.08155099999999
,
"location" :
"lat" : 13.0524139,
"lng" : 80.25082460000002
,
"location_type" : "APPROXIMATE",
"viewport" :
"northeast" :
"lat" : 13.2339843,
"lng" : 80.3322912
,
"southwest" :
"lat" : 12.8339608,
"lng" : 80.08155099999999
,
"types" : [ "locality", "political" ]
,
"address_components" : [
"long_name" : "Tamil Nadu",
"short_name" : "TN",
"types" : [ "administrative_area_level_1", "political" ]
,
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
],
"formatted_address" : "Tamil Nadu, India",
"geometry" :
"bounds" :
"northeast" :
"lat" : 13.496666,
"lng" : 80.3464511
,
"southwest" :
"lat" : 8.077606899999999,
"lng" : 76.23055409999999
,
"location" :
"lat" : 11.1271225,
"lng" : 78.6568942
,
"location_type" : "APPROXIMATE",
"viewport" :
"northeast" :
"lat" : 13.496666,
"lng" : 80.3464511
,
"southwest" :
"lat" : 8.077606899999999,
"lng" : 76.23055409999999
,
"types" : [ "administrative_area_level_1", "political" ]
,
"address_components" : [
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
],
"formatted_address" : "India",
"geometry" :
"bounds" :
"northeast" :
"lat" : 35.5044752,
"lng" : 97.395555
,
"southwest" :
"lat" : 6.747138899999999,
"lng" : 68.1623859
,
"location" :
"lat" : 20.593684,
"lng" : 78.96288
,
"location_type" : "APPROXIMATE",
"viewport" :
"northeast" :
"lat" : 35.5044752,
"lng" : 97.395555
,
"southwest" :
"lat" : 6.747138899999999,
"lng" : 68.1627956
,
"types" : [ "country", "political" ]
],
"status" : "OK"
有时 Json 会这样
"results" : [
"address_components" : [
"long_name" : "State Highway 73A",
"short_name" : "SH 73A",
"types" : [ "route" ]
,
"long_name" : "Madurai",
"short_name" : "Madurai",
"types" : [ "administrative_area_level_2", "political" ]
,
"long_name" : "Tamil Nadu",
"short_name" : "TN",
"types" : [ "administrative_area_level_1", "political" ]
,
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
,
"long_name" : "625706",
"short_name" : "625706",
"types" : [ "postal_code" ]
],
"formatted_address" : "State Highway 73A, Tamil Nadu 625706",
"geometry" :
"bounds" :
"northeast" :
"lat" : 9.865866799999999,
"lng" : 77.8862288
,
"southwest" :
"lat" : 9.8630178,
"lng" : 77.88380289999999
,
"location" :
"lat" : 9.863830799999999,
"lng" : 77.88440989999999
,
"location_type" : "APPROXIMATE",
"viewport" :
"northeast" :
"lat" : 9.865866799999999,
"lng" : 77.8863648302915
,
"southwest" :
"lat" : 9.8630178,
"lng" : 77.88366686970849
,
"types" : [ "route" ]
,
"address_components" : [
"long_name" : "Kalappanpatti",
"short_name" : "Kalappanpatti",
"types" : [ "locality", "political" ]
,
"long_name" : "Madurai",
"short_name" : "Madurai",
"types" : [ "administrative_area_level_2", "political" ]
,
"long_name" : "Tamil Nadu",
"short_name" : "TN",
"types" : [ "administrative_area_level_1", "political" ]
,
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
],
"formatted_address" : "Kalappanpatti, Tamil Nadu, India",
"geometry" :
"bounds" :
"northeast" :
"lat" : 9.878088900000002,
"lng" : 77.86875909999999
,
"southwest" :
"lat" : 9.834436999999999,
"lng" : 77.829544
,
"location" :
"lat" : 9.8540633,
"lng" : 77.85387969999999
,
"location_type" : "APPROXIMATE",
"viewport" :
"northeast" :
"lat" : 9.878088900000002,
"lng" : 77.86875909999999
,
"southwest" :
"lat" : 9.834436999999999,
"lng" : 77.829544
,
"types" : [ "locality", "political" ]
,
"address_components" : [
"long_name" : "625529",
"short_name" : "625529",
"types" : [ "postal_code" ]
,
"long_name" : "Madurai",
"short_name" : "Madurai",
"types" : [ "administrative_area_level_2", "political" ]
,
"long_name" : "Tamil Nadu",
"short_name" : "TN",
"types" : [ "administrative_area_level_1", "political" ]
,
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
],
"formatted_address" : "Tamil Nadu 625529, India",
"geometry" :
"bounds" :
"northeast" :
"lat" : 9.9536049,
"lng" : 77.9122581
,
"southwest" :
"lat" : 9.8377929,
"lng" : 77.8131497
,
"location" :
"lat" : 9.8930471,
"lng" : 77.87102849999999
,
"location_type" : "APPROXIMATE",
"viewport" :
"northeast" :
"lat" : 9.9536049,
"lng" : 77.9122581
,
"southwest" :
"lat" : 9.8377929,
"lng" : 77.8131497
,
"types" : [ "postal_code" ]
,
"address_components" : [
"long_name" : "Madurai",
"short_name" : "Madurai",
"types" : [ "administrative_area_level_2", "political" ]
,
"long_name" : "Tamil Nadu",
"short_name" : "TN",
"types" : [ "administrative_area_level_1", "political" ]
,
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
],
"formatted_address" : "Madurai, Tamil Nadu, India",
"geometry" :
"bounds" :
"northeast" :
"lat" : 10.3264033,
"lng" : 78.4698485
,
"southwest" :
"lat" : 9.553354499999999,
"lng" : 77.4357605
,
"location" :
"lat" : 9.953277900000002,
"lng" : 78.0195387
,
"location_type" : "APPROXIMATE",
"viewport" :
"northeast" :
"lat" : 10.3264033,
"lng" : 78.4698485
,
"southwest" :
"lat" : 9.553354499999999,
"lng" : 77.4357605
,
"types" : [ "administrative_area_level_2", "political" ]
,
"address_components" : [
"long_name" : "Tamil Nadu",
"short_name" : "TN",
"types" : [ "administrative_area_level_1", "political" ]
,
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
],
"formatted_address" : "Tamil Nadu, India",
"geometry" :
"bounds" :
"northeast" :
"lat" : 13.496666,
"lng" : 80.3464511
,
"southwest" :
"lat" : 8.077606899999999,
"lng" : 76.23055409999999
,
"location" :
"lat" : 11.1271225,
"lng" : 78.6568942
,
"location_type" : "APPROXIMATE",
"viewport" :
"northeast" :
"lat" : 13.496666,
"lng" : 80.3464511
,
"southwest" :
"lat" : 8.077606899999999,
"lng" : 76.23055409999999
,
"types" : [ "administrative_area_level_1", "political" ]
,
"address_components" : [
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
],
"formatted_address" : "India",
"geometry" :
"bounds" :
"northeast" :
"lat" : 35.5044752,
"lng" : 97.395555
,
"southwest" :
"lat" : 6.747138899999999,
"lng" : 68.1623859
,
"location" :
"lat" : 20.593684,
"lng" : 78.96288
,
"location_type" : "APPROXIMATE",
"viewport" :
"northeast" :
"lat" : 35.5044752,
"lng" : 97.395555
,
"southwest" :
"lat" : 6.747138899999999,
"lng" : 68.1627956
,
"types" : [ "country", "political" ]
],
"status" : "OK"
我试过了
JSONObject jObject = new JSONObject(s);
JSONObject results= jObject.getJSONObject("results");
JSONObject address= jObject.getJSONObject("address_components");
Map<String,String> map = new HashMap<String,String>();
Iterator iter = address.keys();
while(iter.hasNext())
String key = (String)iter.next();
String value = menu.getString(key);
map.put(key,value);
它没有返回正确的值..
帮帮我。在此先感谢
【问题讨论】:
@Chimbu-只需添加您尝试过的代码以更好地理解 显示您的 JSON 文件以及到目前为止您尝试过的操作。 如果你真的希望你的代码工作,结果似乎是一个 JSONArray,而不是一个 JSONObject。 JSONArray 中的每个索引都是一个 JSONObject 【参考方案1】:这里是工作示例代码
JSONObject jsonObject;
String addre = null;
Address addr1 = null;
try
jsonObject = serverCall.getName(Double.parseDouble(latitude.getText().toString()),
Double.parseDouble(longitude.getText().toString()));
addre = ((JSONArray)jsonObject.get("results")).getJSONObject(0).getString("formatted_address");
if("OK".equalsIgnoreCase(jsonObject.getString("status")))
Log.d("status",jsonObject.getString("status"));
addr1 = new Address(Locale.getDefault());
for(int i=1;i<((JSONArray)jsonObject.get("results")).length()-2;i++)
JSONArray addrComp = ((JSONArray)jsonObject.get("results")).getJSONObject(i).getJSONArray("address_components");
//Toast.makeText(getApplicationContext(),addrComp.toString(),Toast.LENGTH_LONG).show();
// Log.d("Geocoder",addrComp.toString());
for(int j=0;j<addrComp.length();j++)
String neighborhood = ((JSONArray)((JSONObject)addrComp.get(j)).get("types")).getString(0);
if (neighborhood.compareTo("neighborhood") == 0)
String neighborhood1 = ((JSONObject)addrComp.get(j)).getString("long_name");
addr1.setSubThoroughfare(neighborhood1);
String locality = ((JSONArray)((JSONObject)addrComp.get(j)).get("types")).getString(0);
if (locality.compareTo("locality") == 0)
String locality1 = ((JSONObject)addrComp.get(0)).getString("long_name");
addr1.setLocality(locality1);
String subadminArea = ((JSONArray)((JSONObject)addrComp.get(j)).get("types")).getString(0);
if (locality.compareTo("administrative_area_level_2") == 0)
String subadminArea1 = ((JSONObject)addrComp.get(j)).getString("long_name");
addr1.setSubAdminArea(subadminArea1);
String adminArea = ((JSONArray)((JSONObject)addrComp.get(j)).get("types")).getString(0);
if (adminArea.compareTo("administrative_area_level_1") == 0)
String adminArea1 = ((JSONObject)addrComp.get(j)).getString("long_name");
addr1.setAdminArea(adminArea1);
String postalcode = ((JSONArray)((JSONObject)addrComp.get(j)).get("types")).getString(0);
if (postalcode.compareTo("postal_code") == 0)
String postalcode1 = ((JSONObject)addrComp.get(j)).getString("long_name");
addr1.setPostalCode(postalcode1);
String sublocality = ((JSONArray)((JSONObject)addrComp.get(j)).get("types")).getString(0);
if (sublocality.compareTo("sublocality") == 0)
String sublocality1 = ((JSONObject)addrComp.get(j)).getString("long_name");
addr1.setSubLocality(sublocality1);
String countr = ((JSONArray)((JSONObject)addrComp.get(j)).get("types")).getString(0);
if (countr.compareTo("country") == 0)
String countr1 = ((JSONObject)addrComp.get(j)).getString("long_name");
addr1.setCountryName(countr1);
addr_label.setText("Address:"+addre/*+","+addr1.getSubLocality()+","+addr1.getSubThoroughfare()+","+addr1.getLocality()*/);
city.setText("City:"+addr1.getSubAdminArea());
state.setText("State:"+addr1.getAdminArea());
country.setText("Country:"+addr1.getCountryName());
pin.setText("Pin:"+addr1.getPostalCode());
else
Toast.makeText(getApplicationContext(),
"You may check your internet connection or latitude and longitude values",Toast.LENGTH_LONG).show();
catch (JSONException e)
Toast.makeText(getApplicationContext(),
e.toString(),Toast.LENGTH_LONG).show();
e.printStackTrace();
Log.d("JSONException",e.toString());
享受...
【讨论】:
【参考方案2】:作为suggested by user370305:
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1);
String address = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getAddressLine(1);
String country = addresses.get(0).getAddressLine(2);
【讨论】:
@chimbu,这里不是也在后台使用 JSON 格式吗?这只是抽象了传输内容类型,并为您提供了一个很好的对象模型来处理。您仍然可以获得所有可能的地址。对吗? 以上地理编码方法总是抛出异常“服务不可用” code.google.com/p/android/issues/detail?id=38009 请重启您的设备一次。 地理编码器与可用的 API 相比极为有限,它不能替代大多数用途。尝试根据街道名称获取地址:/以上是关于Android:我想以 json 格式从 google map api 中提取地址的主要内容,如果未能解决你的问题,请参考以下文章
当我将我的 rgb 位图转换为 yuv 时,它会产生红色图像。我想以 YUV 格式从图库中读取图像或将 RGB 位图转换为 YUV
如何从 Android 应用程序将 json 文件格式导入 CouchBase lite