Obtener la dirección particular utilizando latitud y longitud
Necesito saber si hay alguna API, desde donde puedo recibir la dirección del lugar actual. Utilizando el Administrador de ubicación, he recibido la latitud y la longitud del lugar actual, pero necesito la dirección.
He intentado el api abajo.
- ¿Dónde estoy? - Obtener país
- Cómo encontrar la estación de policía más cercana a un punto geográfico
- LocationClient vs LocationManager
- Guardar ubicación de red como un archivo .txt (sin utilizar GPS)
- ¿Qué tan preciso y rápido es androids proveedor de ubicación de red?
http://maps.googleapis.com/maps/api/geocode/json?latlng="+ lat + "," + lon + &sensor=true"
Pero no está mostrando el lugar exacto. Puede alguien ayudarme. @ Gracias
- Geolocalización de webview de android
- OnProviderEnabled no funciona?
- ¿Cómo obtener Latitude y Longitud del dispositivo móvil en android?
- Buscar origen de ubicación mediante Fused Location Provider
- ¿Qué tan confiable es LocationLastKnownLocation de LocationManager y con qué frecuencia se actualiza?
- ¿Cómo comprobar si la ubicación está disponible utilizando FusedLocationProviderApi y GoogleClient?
- Navigator.geolocation.getCurrentPosition siempre obtiene un código de error 3: timeout expirado
- Cómo puedo conseguir la localización geográfica actual del dispositivo programatically
Desde un objeto Geocoder , puede llamar al método getFromLocation (double, double, int) .
me gusta :-
private String getAddress(double latitude, double longitude) { StringBuilder result = new StringBuilder(); try { Geocoder geocoder = new Geocoder(this, Locale.getDefault()); List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1); if (addresses.size() > 0) { Address address = addresses.get(0); result.append(address.getLocality()).append("\n"); result.append(address.getCountryName()); } } catch (IOException e) { Log.e("tag", e.getMessage()); } return result.toString(); }
Otra mejor respuesta está aquí ¿Cómo obtener el nombre de la ciudad de las coordenadas de latitud y longitud en Google Maps?
He creado una clase para obtener la dirección de un particular Lat, Long. Puede utilizar esto:
public class getReverseGeoCoding { private String Address1 = "", Address2 = "", City = "", State = "", Country = "", County = "", PIN = ""; public void getAddress() { Address1 = ""; Address2 = ""; City = ""; State = ""; Country = ""; County = ""; PIN = ""; try { JSONObject jsonObj = parser_Json.getJSONfromURL("http://maps.googleapis.com/maps/api/geocode/json?latlng=" + Global.curLatitude + "," + Global.curLongitude + "&sensor=true"); 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 (TextUtils.isEmpty(long_name) == false || !long_name.equals(null) || long_name.length() > 0 || long_name != "") { if (Type.equalsIgnoreCase("street_number")) { Address1 = long_name + " "; } else if (Type.equalsIgnoreCase("route")) { Address1 = Address1 + long_name; } else if (Type.equalsIgnoreCase("sublocality")) { Address2 = long_name; } else if (Type.equalsIgnoreCase("locality")) { // Address2 = Address2 + long_name + ", "; City = long_name; } else if (Type.equalsIgnoreCase("administrative_area_level_2")) { County = long_name; } else if (Type.equalsIgnoreCase("administrative_area_level_1")) { State = long_name; } else if (Type.equalsIgnoreCase("country")) { Country = long_name; } else if (Type.equalsIgnoreCase("postal_code")) { PIN = long_name; } } // JSONArray mtypes = zero2.getJSONArray("types"); // String Type = mtypes.getString(0); // Log.e(Type,long_name); } } } catch (Exception e) { e.printStackTrace(); } } public String getAddress1() { return Address1; } public String getAddress2() { return Address2; } public String getCity() { return City; } public String getState() { return State; } public String getCountry() { return Country; } public String getCounty() { return County; } public String getPIN() { return PIN; } }
JSON PARSER CLASE
public class parser_Json { public static JSONObject getJSONfromURL(String url) { // initialize InputStream is = null; String result = ""; JSONObject jObject = null; // http post try { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); } catch (Exception e) { Log.e("log_tag", "Error in http connection " + e.toString()); } // convert response to string try { BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); result = sb.toString(); } catch (Exception e) { Log.e("log_tag", "Error converting result " + e.toString()); } // try parse the string to a JSON object try { jObject = new JSONObject(result); } catch (JSONException e) { Log.e("log_tag", "Error parsing data " + e.toString()); } return jObject; } }
String longti = "0"; String lati = "0"; LocationManager locationManager; locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, new MyLocationListners()); final Location location = locationManager .getLastKnownLocation(LocationManager.GPS_PROVIDER); //these are your longtitude and latitude lati = String.valueOf(location.getLatitude()); longti = String.valueOf(location.getLongitude()); //here we are getting the address using the geo codes(longtitude and latitude). String ad = getAddress(location.getLatitude(),location.getLongitude()); private String getAddress(double LATITUDE, double LONGITUDE) { String strAdd = ""; Geocoder geocoder = new Geocoder(this, Locale.getDefault()); try { List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1); if (addresses != null) { Address returnedAddress = addresses.get(0); StringBuilder strReturnedAddress = new StringBuilder(""); for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) { strReturnedAddress .append(returnedAddress.getAddressLine(i)).append( "\n"); } strAdd = strReturnedAddress.toString(); Log.w("My Current loction address", "" + strReturnedAddress.toString()); } else { Log.w("My Current loction address", "No Address returned!"); } } catch (Exception e) { e.printStackTrace(); Log.w("My Current loction address", "Canont get Address!"); } return strAdd; } public class MyLocationListners implements LocationListener { @Override public void onLocationChanged(Location location) { } @Override public void onProviderDisabled(String provider) { } @Override public void onProviderEnabled(String provider) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } }
/ / Asegúrese de que los gps y el Internet están en // también a veces, la ubicación no será visible en la primera vez que ejecute el código debido a algunos // problema de servicios de Google por lo que debe reiniciar el teléfono // espero que esto le ayuda
- ¿Qué permiso debe requerir mi receptor SMS de manejo de entrada del sistema?
- Cómo descomprimir el archivo de InputStream