Quiero analizar Google Map API para el geocódigo inverso en Android

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

Este enlace me da el siguiente resultado Este archivo XML no parece tener ninguna información de estilo asociada con él. La estructura del documento se muestra a continuación.

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> 

Quiero buscar el long_name, la localidad y el país del xml he intentado varios postes pero didnt consigue la respuesta perfecta puede cualquier persona por favor me guía, cómo analizar este XML en androide ??

Raghunandan tiene razón. Sin embargo, si su objetivo es simplemente obtener el 'long_name', etc localidad utilizando Google Maps API, es posible que desee probar de la siguiente manera para obtenerlo. Usted puede obtener la respuesta es en forma de JSON que me siento más estructurado. Puedes probar primero la URL: http://maps.google.com/maps/api/geocode/json?latlng=23.0043673,72.5411868999996&sensor=false

Yo uso el siguiente método todo el tiempo y funciona para mí. Podría ayudarle a conseguir lo que está intentando:

Puede obtener la latitud y la longitud y luego solicitar servidores de google, para responder con un objeto JSON que contiene información variada sobre las coordenadas de ubicación. Aquí está la función:

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

Ahora puedes analizar el JSON para obtener lo que quieras. Por ejemplo, supongamos que desea completar la dirección formateada, aquí es cómo puede obtener:

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

Si desea saber más acerca de cómo extraer los datos deseados del tipo de JSON que obtendrá en respuesta, consulte las respuestas en Android – Cómo analizar JSONObject y JSONArrays pregunta. Espero que te ayude.

Actualización :

Espero que hayas leído las respuestas en la pregunta anterior. Ahora, Por ejemplo: Si desea obtener JSON Array de address_components , simplemente haga algo como

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

Ahora puede obtener el primer nombre long_name siguiente manera:

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

Así que trate de ver comparar el patrón aquí con la respuesta JSON que está recibiendo. Esto le hará entender cómo acercarse para obtener cualquier elemento. Aquí, el result fue JSONARray, así que lo tengo como JSONArray. Entonces tomé el primer objeto del result JSONArray cuyo name era address_components y el value era otro JSONArray. Entonces porque quería obtener el primer nombre long_name , así que tomé de nuevo el primer objeto del address_component address_component y luego pedí que obtuviera el value del componente cuyo nombre era long_name . Espero que ahora entiendas un poco cómo puedes leer valores.

PD: Yo no corrí las dos últimas líneas, pero formulado lógicamente. Espero que te haga entender. ¡Todo lo mejor!

Manera simple de conseguir el resultado exacto. Espero eso ayude !

Acaba de poner esta línea donde desee la dirección:

 String Adress_final=new GeocodeAPI().execute().get(); 

A continuación, agregue esto,

  //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;** 
  • Apóstrofe no precedido por \
  • No se detectaron restricciones de gramática (DTD o esquema XML) para el documento (Android)
  • ¿Cómo enviar la solicitud SOAP y analizar la respuesta SOAP en formato XML en Android?
  • ¿Cómo puedo analizar XML android?
  • Análisis del archivo de manifiesto de Android para buscar la etiqueta uses-permission usando python
  • XmlPullParser no funciona con InputStream
  • La forma más fácil de escribir y leer un XML
  • No se pudo obtener los datos xml de res en Android 3.0 y versiones posteriores
  • XmlPullParser: PI no debe comenzar con xml, pero solo en Android 2.3.3 hacia abajo
  • Sax - ExpatParser $ ParseException
  • Diferencia entre setContentView y LayoutInflater
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.