通过复杂的Google Geocoding JSON对象中的键获取值-Java-android

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过复杂的Google Geocoding JSON对象中的键获取值-Java-android相关的知识,希望对你有一定的参考价值。

我使用此有用的code snippet来获取我的android应用程序中的当前位置,现在我想获取route(街道地址),street_numberpostal_codecountry(始终为long_name )在Google Geocoding JSON对象中。目前,我使用do-while循环,然后拆分formatted_address以得到所需的结果,但我认为最好通过其键访问该值。

JSON看起来像这样:


"results": [
    
        "types": [
            "street_address"
        ],
        "formatted_address": "RotenturmstraÃe 2, 1010 Vienna, Austria",
        "address_components": [
            
                "types": [
                    "street_number"
                ],
                "short_name": "2",
                "long_name": "2"
            ,
            
                "types": [
                    "route"
                ],
                "short_name": "Rotenturmstr.",
                "long_name": "RotenturmstraÃe"
            ,
            
                "types": [
                    "sublocality_level_1",
                    "sublocality",
                    "political"
                ],
                "short_name": "Innere Stadt",
                "long_name": "Innere Stadt"
            ,
            
                "types": [
                    "locality",
                    "political"
                ],
                "short_name": "Vienna",
                "long_name": "Vienna"
            ,
            
                "types": [
                    "administrative_area_level_1",
                    "political"
                ],
                "short_name": "Vienna",
                "long_name": "Vienna"
            ,
            
                "types": [
                    "country",
                    "political"
                ],
                "short_name": "AT",
                "long_name": "Austria"
            ,
            
                "types": [
                    "postal_code"
                ],
                "short_name": "1010",
                "long_name": "1010"
            
        ],
        "geometry": 
            "viewport": 
                "southwest": 
                    "lng": 16.3719497197085,
                    "lat": 48.20797491970851
                ,
                "northeast": 
                    "lng": 16.3746476802915,
                    "lat": 48.2106728802915
                
            ,
            "location": 
                "lng": 16.3732987,
                "lat": 48.2093239
            ,
            "location_type": "ROOFTOP"
        
    ,
    
        "types": [
            "neighborhood",
            "political"
        ],
        "formatted_address": "Stephansdom, 1010 Vienna, Austria",
        "address_components": [
            
                "types": [
                    "neighborhood",
                    "political"
                ],
                "short_name": "Stephansdom",
                "long_name": "Stephansdom"
            ,
            
                "types": [
                    "sublocality_level_1",
                    "sublocality",
                    "political"
                ],
                "short_name": "Innere Stadt",
                "long_name": "Innere Stadt"
            ,
            
                "types": [
                    "locality",
                    "political"
                ],
                "short_name": "Vienna",
                "long_name": "Vienna"
            ,
            
                "types": [
                    "administrative_area_level_1",
                    "political"
                ],
                "short_name": "Vienna",
                "long_name": "Vienna"
            ,
            
                "types": [
                    "country",
                    "political"
                ],
                "short_name": "AT",
                "long_name": "Austria"
            ,
            
                "types": [
                    "postal_code"
                ],
                "short_name": "1010",
                "long_name": "1010"
            
        ],
        "geometry": 
            "bounds": 
                "southwest": 
                    "lng": 16.3663459,
                    "lat": 48.2016949
                ,
                "northeast": 
                    "lng": 16.382804,
                    "lat": 48.21546559999999
                
            ,
            "viewport": 
                "southwest": 
                    "lng": 16.3663459,
                    "lat": 48.2016949
                ,
                "northeast": 
                    "lng": 16.382804,
                    "lat": 48.21546559999999
                
            ,
            "location": 
                "lng": 16.3767451,
                "lat": 48.2083206
            ,
            "location_type": "APPROXIMATE"
        
    ,
    
        "types": [
            "postal_code"
        ],
        "formatted_address": "1010 Vienna, Austria",
        "address_components": [
            
                "types": [
                    "postal_code"
                ],
                "short_name": "1010",
                "long_name": "1010"
            ,
            
                "types": [
                    "locality",
                    "political"
                ],
                "short_name": "Vienna",
                "long_name": "Vienna"
            ,
            
                "types": [
                    "administrative_area_level_1",
                    "political"
                ],
                "short_name": "Vienna",
                "long_name": "Vienna"
            ,
            
                "types": [
                    "country",
                    "political"
                ],
                "short_name": "AT",
                "long_name": "Austria"
            
        ],
        "geometry": 
            "bounds": 
                "southwest": 
                    "lng": 16.3552218,
                    "lat": 48.1992606
                ,
                "northeast": 
                    "lng": 16.3853131,
                    "lat": 48.2185876
                
            ,
            "viewport": 
                "southwest": 
                    "lng": 16.3552218,
                    "lat": 48.1992606
                ,
                "northeast": 
                    "lng": 16.3853131,
                    "lat": 48.2185876
                
            ,
            "location": 
                "lng": 16.3677601,
                "lat": 48.2083556
            ,
            "location_type": "APPROXIMATE"
        
    ,
    
        "types": [
            "sublocality_level_1",
            "sublocality",
            "political"
        ],
        "formatted_address": "Innere Stadt, 1010 Vienna, Austria",
        "address_components": [
            
                "types": [
                    "sublocality_level_1",
                    "sublocality",
                    "political"
                ],
                "short_name": "Innere Stadt",
                "long_name": "Innere Stadt"
            ,
            
                "types": [
                    "locality",
                    "political"
                ],
                "short_name": "Vienna",
                "long_name": "Vienna"
            ,
            
                "types": [
                    "administrative_area_level_1",
                    "political"
                ],
                "short_name": "Vienna",
                "long_name": "Vienna"
            ,
            
                "types": [
                    "country",
                    "political"
                ],
                "short_name": "AT",
                "long_name": "Austria"
            ,
            
                "types": [
                    "postal_code"
                ],
                "short_name": "1010",
                "long_name": "1010"
            
        ],
        "geometry": 
            "bounds": 
                "southwest": 
                    "lng": 16.3552218,
                    "lat": 48.1992606
                ,
                "northeast": 
                    "lng": 16.3853131,
                    "lat": 48.2185876
                
            ,
            "viewport": 
                "southwest": 
                    "lng": 16.3552218,
                    "lat": 48.1992606
                ,
                "northeast": 
                    "lng": 16.3853131,
                    "lat": 48.2185876
                
            ,
            "location": 
                "lng": 16.3697428,
                "lat": 48.2088774
            ,
            "location_type": "APPROXIMATE"
        
    ,
    
        "types": [
            "administrative_area_level_1",
            "political"
        ],
        "formatted_address": "Vienna, Austria",
        "address_components": [
            
                "types": [
                    "administrative_area_level_1",
                    "political"
                ],
                "short_name": "Vienna",
                "long_name": "Vienna"
            ,
            
                "types": [
                    "country",
                    "political"
                ],
                "short_name": "AT",
                "long_name": "Austria"
            
        ],
        "geometry": 
            "bounds": 
                "southwest": 
                    "lng": 16.1826199,
                    "lat": 48.1182699
                ,
                "northeast": 
                    "lng": 16.5774999,
                    "lat": 48.3230999
                
            ,
            "viewport": 
                "southwest": 
                    "lng": 16.1826199,
                    "lat": 48.1182699
                ,
                "northeast": 
                    "lng": 16.5774999,
                    "lat": 48.3230999
                
            ,
            "location": 
                "lng": 16.3738138,
                "lat": 48.20823499999999
            ,
            "location_type": "APPROXIMATE"
        
    
]

这是我的代码:

public static JSONObject getLocationInfo(double lat, double lng) 

    HttpGet httpGet = new HttpGet("http://maps.googleapis.com/maps/api/geocode/json?latlng="+ lat+","+lng +"&sensor=true");
    HttpClient client = new DefaultHttpClient();
    HttpResponse response;
    StringBuilder stringBuilder = new StringBuilder();

    try 
        response = client.execute(httpGet);
        HttpEntity entity = response.getEntity();
        InputStream stream = entity.getContent();
        int b;
        while ((b = stream.read()) != -1) 
            stringBuilder.append((char) b);
        
     catch (ClientProtocolException e) 
     catch (IOException e) 
    

    JSONObject jsonObject = new JSONObject();
    try 
        jsonObject = new JSONObject(stringBuilder.toString());
     catch (JSONException e) 
        e.printStackTrace();
    

    return jsonObject;


public static String getCurrentLocationViaJSON(double lat, double lng) 

    JSONObject jsonObj = getLocationInfo(lat, lng);
    Log.i("JSON string =>", jsonObj.toString());

    String currentLocation = "testing";
    String street_address = null;
    String postal_code = null; 

    try 
        String status = jsonObj.getString("status").toString();
        Log.i("status", status);

        if(status.equalsIgnoreCase("OK"))
            JSONArray results = jsonObj.getJSONArray("results");
            int i = 0;
            Log.i("i", i+ "," + results.length() ); //TODO delete this
            do

                JSONObject r = results.getJSONObject(i);
                JSONArray typesArray = r.getJSONArray("types");
                String types = typesArray.getString(0);

                if(types.equalsIgnoreCase("street_address"))
                    street_address = r.getString("formatted_address").split(",")[0];
                    Log.i("street_address", street_address);
                else if(types.equalsIgnoreCase("postal_code"))
                    postal_code = r.getString("formatted_address");
                    Log.i("postal_code", postal_code);
                

                if(street_address!=null && postal_code!=null)
                    currentLocation = street_address + "," + postal_code;
                    Log.i("Current Location =>", currentLocation); //Delete this
                    i = results.length();
                

                i++;
            while(i<results.length());

            Log.i("JSON Geo Locatoin =>", currentLocation);
            return currentLocation;
        

     catch (JSONException e) 
        Log.e("testing","Failed to load JSON");
        e.printStackTrace();
    
    return null;

任何帮助将不胜感激。

答案
在该代码示例中,它们使用JSONObject,因此您将要使用getJSONObject(i)getJSONArray()getString()解析数据。
另一答案
此代码将始终为您提供地址。已验证!

以上是关于通过复杂的Google Geocoding JSON对象中的键获取值-Java-android的主要内容,如果未能解决你的问题,请参考以下文章

Google Geocoding API 返回错误的地址,但 Google 地图显示正确的位置

Python Google Geocoding API - IndexError:列表索引超出范围?

php Ejemplo API Rest - Google Maps Geocoding

Google Maps Geocoding API 使用限制

Google Maps Geocoding API,其 JS api 中缺少的 API 功能(?)

在哪里放置 Google Geocoding API 的 API 密钥?