Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我想解析Android中反向地理代码的谷歌地图API_Java_Android_Xml Parsing_Reverse Geocoding - Fatal编程技术网

Java 我想解析Android中反向地理代码的谷歌地图API

Java 我想解析Android中反向地理代码的谷歌地图API,java,android,xml-parsing,reverse-geocoding,Java,Android,Xml Parsing,Reverse Geocoding,这个链接给了我以下结果 此XML文件似乎没有任何与之关联的样式信息。文档树如下所示 This XML file does not appear to have any style information associated with it. The document tree is shown below. <GeocodeResponse> <status>OK</status> <result> <type>route</t

这个链接给了我以下结果 此XML文件似乎没有任何与之关联的样式信息。文档树如下所示

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<GeocodeResponse>
<status>OK</status>
<result>
<type>route</type>
<formatted_address>
Dharnidhar Road, Vasna, Ahmedabad, Gujarat 380007, India
</formatted_address>
<address_component>
<long_name>Dharnidhar Road</long_name>
<short_name>Dharnidhar Road</short_name>
<type>route</type>
</address_component>
<address_component>
<long_name>Vasna</long_name>
<short_name>Vasna</short_name>
<type>sublocality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Ahmedabad</long_name>
<short_name>Ahmedabad</short_name>
<type>locality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Ahmedabad</long_name>
<short_name>Ahmedabad</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Gujarat</long_name>
<short_name>GJ</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>India</long_name>
<short_name>IN</short_name>
<type>country</type>
<type>political</type>
</address_component>
<address_component>
<long_name>380007</long_name>
<short_name>380007</short_name>
<type>postal_code</type>
</address_component>
<geometry>
<location>
<lat>23.0043870</lat>
<lng>72.5419184</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>23.0030380</lat>
<lng>72.5405695</lng>
</southwest>
<northeast>
<lat>23.0057360</lat>
<lng>72.5432674</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>23.0041405</lat>
<lng>72.5414125</lng>
</southwest>
<northeast>
<lat>23.0046335</lat>
<lng>72.5424244</lng>
</northeast>
</bounds>
</geometry>
</result>
<result>
<type>neighborhood</type>
<type>political</type>
<formatted_address>
Yashkamal Society, Ahmedabad, Gujarat 380007, India
</formatted_address>
<address_component>
<long_name>Yashkamal Society</long_name>
<short_name>Yashkamal Society</short_name>
<type>neighborhood</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Ahmedabad</long_name>
<short_name>Ahmedabad</short_name>
<type>locality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Ahmedabad</long_name>
<short_name>Ahmedabad</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Gujarat</long_name>
<short_name>GJ</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>India</long_name>
<short_name>IN</short_name>
<type>country</type>
<type>political</type>
</address_component>
<address_component>
<long_name>380007</long_name>
<short_name>380007</short_name>
<type>postal_code</type>
</address_component>
<geometry>
<location>
<lat>23.0089040</lat>
<lng>72.5446990</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>23.0016970</lat>
<lng>72.5373369</lng>
</southwest>
<northeast>
<lat>23.0125600</lat>
<lng>72.5510640</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>23.0016970</lat>
<lng>72.5373369</lng>
</southwest>
<northeast>
<lat>23.0125600</lat>
<lng>72.5510640</lng>
</northeast>
</bounds>
</geometry>
</result>
<result>
<type>sublocality</type>
<type>political</type>
<formatted_address>Vasna, Ahmedabad, Gujarat, India</formatted_address>
<address_component>
<long_name>Vasna</long_name>
<short_name>Vasna</short_name>
<type>sublocality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Ahmedabad</long_name>
<short_name>Ahmedabad</short_name>
<type>locality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Ahmedabad</long_name>
<short_name>Ahmedabad</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Gujarat</long_name>
<short_name>GJ</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>India</long_name>
<short_name>IN</short_name>
<type>country</type>
<type>political</type>
</address_component>
<geometry>
<location>
<lat>23.0043673</lat>
<lng>72.5411869</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>22.9964810</lat>
<lng>72.5357220</lng>
</southwest>
<northeast>
<lat>23.0125600</lat>
<lng>72.5530250</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>22.9964810</lat>
<lng>72.5357220</lng>
</southwest>
<northeast>
<lat>23.0125600</lat>
<lng>72.5530250</lng>
</northeast>
</bounds>
</geometry>
</result>
<result>
<type>postal_code</type>
<formatted_address>Ahmedabad, Gujarat 380007, India</formatted_address>
<address_component>
<long_name>380007</long_name>
<short_name>380007</short_name>
<type>postal_code</type>
</address_component>
<address_component>
<long_name>Ahmedabad</long_name>
<short_name>Ahmedabad</short_name>
<type>locality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Ahmedabad</long_name>
<short_name>Ahmedabad</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Gujarat</long_name>
<short_name>GJ</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>India</long_name>
<short_name>IN</short_name>
<type>country</type>
<type>political</type>
</address_component>
<geometry>
<location>
<lat>22.9967321</lat>
<lng>72.5500596</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>22.9810681</lat>
<lng>72.5317127</lng>
</southwest>
<northeast>
<lat>23.0220193</lat>
<lng>72.5760833</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>22.9810681</lat>
<lng>72.5317127</lng>
</southwest>
<northeast>
<lat>23.0220193</lat>
<lng>72.5760833</lng>
</northeast>
</bounds>
</geometry>
</result>
<result>
<type>locality</type>
<type>political</type>
<formatted_address>Ahmedabad, Gujarat, India</formatted_address>
<address_component>
<long_name>Ahmedabad</long_name>
<short_name>Ahmedabad</short_name>
<type>locality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Ahmedabad</long_name>
<short_name>Ahmedabad</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Gujarat</long_name>
<short_name>GJ</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>India</long_name>
<short_name>IN</short_name>
<type>country</type>
<type>political</type>
</address_component>
<geometry>
<location>
<lat>23.0395677</lat>
<lng>72.5660045</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>22.9028759</lat>
<lng>72.4541115</lng>
</southwest>
<northeast>
<lat>23.1378156</lat>
<lng>72.7026413</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>22.9028759</lat>
<lng>72.4541115</lng>
</southwest>
<northeast>
<lat>23.1378156</lat>
<lng>72.7026413</lng>
</northeast>
</bounds>
</geometry>
</result>
<result>
<type>administrative_area_level_2</type>
<type>political</type>
<formatted_address>Ahmedabad, Gujarat, India</formatted_address>
<address_component>
<long_name>Ahmedabad</long_name>
<short_name>Ahmedabad</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Gujarat</long_name>
<short_name>GJ</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>India</long_name>
<short_name>IN</short_name>
<type>country</type>
<type>political</type>
</address_component>
<geometry>
<location>
<lat>23.0395677</lat>
<lng>72.5660045</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>22.0185745</lat>
<lng>71.5979016</lng>
</southwest>
<northeast>
<lat>23.4630890</lat>
<lng>72.8419331</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>22.0185745</lat>
<lng>71.5979016</lng>
</southwest>
<northeast>
<lat>23.4630890</lat>
<lng>72.8419331</lng>
</northeast>
</bounds>
</geometry>
</result>
<result>
<type>administrative_area_level_1</type>
<type>political</type>
<formatted_address>Gujarat, India</formatted_address>
<address_component>
<long_name>Gujarat</long_name>
<short_name>GJ</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>India</long_name>
<short_name>IN</short_name>
<type>country</type>
<type>political</type>
</address_component>
<geometry>
<location>
<lat>22.2586520</lat>
<lng>71.1923805</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>20.1279540</lat>
<lng>68.1628352</lng>
</southwest>
<northeast>
<lat>24.7057090</lat>
<lng>74.4764881</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>20.1279540</lat>
<lng>68.1628342</lng>
</southwest>
<northeast>
<lat>24.7057090</lat>
<lng>74.4764881</lng>
</northeast>
</bounds>
</geometry>
</result>
<result>
<type>country</type>
<type>political</type>
<formatted_address>India</formatted_address>
<address_component>
<long_name>India</long_name>
<short_name>IN</short_name>
<type>country</type>
<type>political</type>
</address_component>
<geometry>
<location>
<lat>20.5936840</lat>
<lng>78.9628800</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>6.7471389</lat>
<lng>68.1627956</lng>
</southwest>
<northeast>
<lat>35.5043404</lat>
<lng>97.3955550</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>6.7471389</lat>
<lng>68.1623859</lng>
</southwest>
<northeast>
<lat>35.5043404</lat>
<lng>97.3955550</lng>
</northeast>
</bounds>
</geometry>
</result>
</GeocodeResponse>
此XML文件似乎没有任何与之关联的样式信息。文档树如下所示。
好啊
路线
印度古吉拉特邦艾哈迈达巴德瓦斯纳达尔尼达尔路380007号
达尼达尔路
达尼达尔路
路线
瓦斯纳
瓦斯纳
亚局部
政治的
艾哈迈达巴德
艾哈迈达巴德
地点
政治的
艾哈迈达巴德
艾哈迈达巴德
行政区二级
政治的
古吉拉特邦
GJ
行政区一级
政治的
印度
在里面
国家
政治的
380007
380007
邮政编码
23.0043870
72.5419184
近似
23.0030380
72.5405695
23.0057360
72.5432674
23.0041405
72.5414125
23.0046335
72.5424244
邻里
政治的
雅什卡马尔协会,艾哈迈达巴德,古吉拉特邦380007,印度
亚什卡马尔学会
亚什卡马尔学会
邻里
政治的
艾哈迈达巴德
艾哈迈达巴德
地点
政治的
艾哈迈达巴德
艾哈迈达巴德
行政区二级
政治的
古吉拉特邦
GJ
行政区一级
政治的
印度
在里面
国家
政治的
380007
380007
邮政编码
23.0089040
72.5446990
近似
23.0016970
72.5373369
23.0125600
72.5510640
23.0016970
72.5373369
23.0125600
72.5510640
亚局部
政治的
瓦斯纳,艾哈迈达巴德,古吉拉特邦,印度
瓦斯纳
瓦斯纳
亚局部
政治的
艾哈迈达巴德
艾哈迈达巴德
地点
政治的
艾哈迈达巴德
艾哈迈达巴德
行政区二级
政治的
古吉拉特邦
GJ
行政区一级
政治的
印度
在里面
国家
政治的
23.0043673
72.5411869
近似
22.9964810
72.5357220
23.0125600
72.5530250
22.9964810
72.5357220
23.0125600
72.5530250
邮政编码
艾哈迈达巴德,古吉拉特邦380007,印度
380007
380007
邮政编码
艾哈迈达巴德
艾哈迈达巴德
地点
政治的
艾哈迈达巴德
艾哈迈达巴德
行政区二级
政治的
古吉拉特邦
GJ
行政区一级
政治的
印度
在里面
国家
政治的
22.9967321
72.5500596
近似
22.9810681
72.5317127
23.0220193
72.5760833
22.9810681
72.5317127
23.0220193
72.5760833
地点
政治的
印度古吉拉特邦艾哈迈达巴德
艾哈迈达巴德
艾哈迈达巴德
地点
政治的
艾哈迈达巴德
艾哈迈达巴德
行政区二级
政治的
古吉拉特邦
GJ
行政区一级
政治的
印度
在里面
国家
政治的
23.0395677
72.5660045
近似
22.9028759
72.4541115
23.1378156
72.7026413
22.9028759
72.4541115
23.1378156
72.7026413
行政区二级
政治的
印度古吉拉特邦艾哈迈达巴德
艾哈迈达巴德
艾哈迈达巴德
行政区二级
政治的
古吉拉特邦
GJ
行政区一级
政治的
印度
在里面
国家
政治的
23.0395677
72.5660045
近似
22.0185745
71.5979016
23.4630890
72.8419331
22.0185745
71.5979016
23.4630890
72.8419331
行政区一级
政治的
印度古吉拉特邦
古吉拉特邦
GJ
行政区一级
政治的
印度
在里面
国家
政治的
22.2586520
71.1923805
近似
20.1279540
68.1628352
24.7057090
74.4764881
20.1279540
68.1628342
24.7057090
74.4764881
国家
政治的
印度
印度
在里面
国家
政治的
20.5936840
78.9628800
近似
6.7471389
68.1627956
35.5043404
97.3955550
6.7471389
68.1623859
35.5043404
97.3955550
我想从xml中获取long_名称、地区和国家 我试过各种帖子,但没有得到完美的答案
有谁能告诉我,如何将这个XML解析成android吗?

Raghunandan是对的。但是,如果您的目标是使用GoogleMapsAPI获取“long_name”、位置等,那么您可能需要尝试以下方法来获取它。您可以得到JSON格式的响应,我觉得它更结构化。您可以先尝试URL:

我一直使用下面的方法,它对我很有效。它可能会帮助您获得您正在尝试的:

您可以获取纬度和经度,然后请求google服务器使用包含位置坐标各种信息的JSON对象进行回复。以下是函数:

public JSONObject getLocationInfo( double lat, double lng) {

    HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?latlng="+lat+","+lng+"&sensor=false");
    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;
}
现在,您可以解析JSON以获得所需的任何内容。例如,假设您想要完整的格式化_地址,下面是您如何获得:

// get lat and lng value
JSONObject ret = getLocationInfo(lat, lng); 
JSONObject location;
String location_string;
try {
    //Get JSON Array called "results" and then get the 0th complete object as JSON        
    location = ret.getJSONArray("results").getJSONObject(0); 
    // Get the value of the attribute whose name is "formatted_string"
    location_string = location.getString("formatted_address");
    Log.d("test", "formattted address:" + location_string);
} catch (JSONException e1) {
    e1.printStackTrace();

}
如果您想更多地了解如何从您将得到的JSON类型中提取所需数据,请参阅问题的答案。希望对你有帮助

更新

希望你已经阅读了以上问题的答案。现在,例如:如果您想要获得
地址\ u组件的JSON数组,那么只需执行以下操作

JSONArray addressComp = ret.getJSONArray("results").getJSONObject(0).getJSONArray("address_components");
现在您可以获得第一个
长\u名称
,如下所示:

String long_name1 = addressComp.getJSONObject(0).getString("long_name");
因此,请尝试将这里的模式与您得到的JSON响应进行比较。这将使您了解如何获取任何元素。这里,
result
是JSONARray,所以我把它命名为JSONARray。然后我取了
结果
JSONArray的第一个对象,它的
名称
地址_组件
,而
是另一个JSONArray。然后,因为我想获取第一个
long\u名称
,所以我再次获取
address\u组件的第一个对象
JSONArray,然后要求获取名称为
long\u名称
的组件的
。希望您现在了解一些如何读取值


附言:我没有运行最后两行,但逻辑公式。希望它能让你明白。祝你一切顺利

获得准确结果的简单方法。希望有帮助

只要把这一行放在你想要的地址:

String Adress_final=new GeocodeAPI().execute().get();
再加上这个,

    //calling web geocoder api to get the address

    protected class GeocodeAPI extends AsyncTask<String, String, String>
    {

        String participantURL = Config.GooglePlacesAPI+"latlng="+Lat+","+Lng+"&sensor=false";
        JSONObject json;

        @Override
        protected void onPreExecute() 
        {
            System.out.println("participantURL==>>"+participantURL);
            // Create a progressdialog mProgressDialog
            mProgressDialog = new ProgressDialog(getActivity());
            // Set progressdialog title
            mProgressDialog.setTitle("Please Wait");
            // Set progressdialog message
            mProgressDialog.setMessage("Loading Location...");
            mProgressDialog.setIndeterminate(false);
            // Show progressdialog
            mProgressDialog.show();
        }


        @Override
        protected String doInBackground(String... params) {
            JSONParser jsonParser = new JSONParser();
            System.out.println("participantURL==>>"+participantURL);
            System.out.println("latitude & Longitude=="+Lat+","+Lng);
            List<NameValuePair> parameters = new ArrayList<NameValuePair>();


            try {
                json = jsonParser.getJSONFromUrl(participantURL, parameters);
                System.out.println("********Return from JSONParser class********");
                System.out.println("json=="+json);

                if (json != null) {

                    // get lat and lng value

                    JSONObject location;

                    try {
                        //Get JSON Array called "results" and then get the 0th complete object as JSON        
                        location = json.getJSONArray("results").getJSONObject(0);
                        System.out.println("location==>>>"+location);
                        // Get the value of the attribute whose name is "formatted_string"
                        location_string = location.getString("formatted_address");
                        System.out.println("location_string==>>>"+location_string);
                        Log.d("test", "formattted address:" + location_string);
                    } catch (JSONException e1) {
                        e1.printStackTrace();
                    }

                }
                }catch (Exception error) {
                    error.getMessage();
                }


            return location_string;**
//调用web geocoder api以获取地址
受保护类GeocodeAPI扩展异步任务
{
字符串participantURL=Config.GooglePlacesAPI+“latlng=“+Lat+”,“+Lng+”&传感器=false”;
JSONObject json;
@凌驾
受保护的void onPreExecute()
{
System.out.println(“participantURL==>>”+participantURL);
//创建progressdialog mProgressDialog
mProgressDialog=newprogressdialog(getActivity());
//设置进程对话框标题
mProgressDialog.setTitle(“请稍候”);