2

在我的 android 应用程序中:我想像使用地理编码器一样提取位置、行政区域、国家名称、邮政编码、地址行,但我无法从谷歌地图 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);
     }

它没有返回正确的值..

帮我。提前致谢

4

2 回答 2

7

这是工作示例代码

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());
                    }

享受...

于 2013-09-26T10:39:04.997 回答
2

正如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);
于 2013-09-23T11:08:18.160 回答