gpt4 book ai didi

java - 我想在 Android 中解析 Google Map API 以进行反向地理编码

转载 作者:行者123 更新时间:2023-11-30 02:30:00 24 4
gpt4 key购买 nike

http://maps.googleapis.com/maps/api/geocode/xml?latlng=23.0043673,72.5411868999996&sensor=false

这个链接给了我以下结果此 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 中获取 long_name、地区和国家我尝试了各种帖子,但没有得到完美的答案任何人都可以指导我如何将这个 XML 解析为 android 吗??

最佳答案

Raghunandan 是对的。但是,如果您的目标只是使用 Google map API 获取“long_name”、位置等,您可能想尝试以下方法来获取它。您可以获得 JSON 形式的响应,我觉得这种形式更有条理。您可以自己先尝试 URL:http://maps.google.com/maps/api/geocode/json?latlng=23.0043673,72.5411868999996&sensor=false

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

您可以获取纬度和经度,然后请求谷歌服务器,以返回包含有关位置坐标的各种信息的 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 以获得您想要的任何内容。例如,假设您想要完整的 formatted_address,可以通过以下方式获得:

// 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 类型中提取所需数据的信息,请参阅 Android - How to parse JSONObject and JSONArrays 上的答案。问题。希望对你有帮助。

更新:

希望您已经阅读了上述问题的答案。现在,例如:如果你想获取 address_components 的 JSON 数组,那么只需执行类似

的操作
JSONArray addressComp = ret.getJSONArray("results").getJSONObject(0).getJSONArray("address_components");

现在可以得到第一个long_name如下:

String long_name1 = addressComp.getJSONObject(0).getString("long_name");

因此,请尝试将此处的模式与您获得的 JSON 响应进行比较。这将使您了解如何获取任何元素。在这里,result 是 JSONARray,所以我得到它作为 JSONArray。然后我获取了 result JSONArray 的第一个对象,其 nameaddress_componentsvalue 是另一个 JSONArray。然后因为想获取第一个long_name,所以又把address_component JSONArray的第一个object拿过来,然后要求获取value的名称为 long_name 的组件。希望现在您对如何读取值有所了解。

P.S:我没有运行最后两行,而是逻辑地表述。希望能让你明白。一切顺利!

关于java - 我想在 Android 中解析 Google Map API 以进行反向地理编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27440718/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com