如何在谷歌地理编码API只得到国家名称(administrative_area_level_1) [英] How to get only State Name (administrative_area_level_1) in Google Geocoding API

查看:902
本文介绍了如何在谷歌地理编码API只得到国家名称(administrative_area_level_1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发必须实现谷歌地理code获得国家域名(administrative_area_level_1)Android应用程序。
为了得到它,我已经能够得到的经度和使用Android中通常的LocationManager纬度。现在我需要做的就是通过lat和长到谷歌地理code网址是这样的:

I am developing an android application which has to implement the Google Geocode to get the state name (administrative_area_level_1). To get it, I've already able to get the longitude and latitude using the usual LocationManager in Android. Now what I need to do is just to pass the lat and long on to Google Geocode url like this:

的https://maps.googleapis .COM /地图/ API /地理code / JSON?经纬度= 40.714224,-73.961452

在结果网址上面显示(JSON和XML)与一些结果和类型的位置。
我想实现的就是让administrative_area_level_1(国家名称)的名称在JSON显示的第一个结果的价值。

In result, url above shows (JSON and XML) with several results and types of location. What I want to achieve is to get the name of administrative_area_level_1 (state name) value of the first result shown in the JSON.

我怎样才能做到这一点?是否有可能实现这一目标?
请帮帮忙,谢谢

How can I achieve this? Is it possible to achieve this? Please help, thanks

推荐答案

试试这个code摆脱谷歌API的状态名称。

try this code to get the state name from google api .

JSONObject jsonObj = new JSONObject(jsonResults.toString());
         String Status = jsonObj.getString("status");
        if (Status.equalsIgnoreCase("OK")) 
        {
           JSONArray Results = jsonObj.getJSONArray("results");
           JSONObject zero = Results.getJSONObject(0);
           JSONArray address_components = zero.getJSONArray("address_components");

           for (int i = 0; i < address_components.length(); i++) 
           {
               JSONObject zero2 = address_components.getJSONObject(i);
               String long_name = zero2.getString("long_name");
               JSONArray mtypes = zero2.getJSONArray("types");
               String Type = mtypes.getString(0);

               if (Type.equalsIgnoreCase("locality")) 
               {
                  City = long_name;


                  Log.e("current city name:","city:"+City);


               } 
               else
               {
                   if (Type.equalsIgnoreCase("administrative_area_level_2")) 
                   {
                      County = long_name;
                   } 
                   else if (Type.equalsIgnoreCase("administrative_area_level_1")) 
                   {
                     State = long_name;


                     Log.e("current city name:","administrative_area_level_1:"+State);
                   } 
                   else if (Type.equalsIgnoreCase("country")) 
                   {
                     Country = long_name;
                   } 
               }
           }
        }

这篇关于如何在谷歌地理编码API只得到国家名称(administrative_area_level_1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆