Cómo obtener el nombre de la ciudad por latitud y longitud en android?
Quiero nombre de la ciudad por ubicación actual, pero tengo latitud y longitude.how para obtenerlo? Utilicé el tecleo del botón entonces consigo la latitud y la longitud del doble valor. Mi código en below.please me ayuda.
¡¡Gracias!!
- Cómo obtener sellos horarios precisos desde la ubicación GPS de Android
- ACCESS_FINE_LOCATION Permisos de AndroidManifest no concedidos
- Cómo falsificar la ubicación para hacer pruebas con LocationClient y Geofences en Android?
- IllegalArgumentException: el proveedor no exisit: null en Maps V1
- Android- misma ubicación GPS repetir sin actualizar
Button btnShowLocation; GPSTracker gps; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btnShowLocation = (Button) findViewById(R.id.btnShowLocation); // show location button click event btnShowLocation.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // create class object gps = new GPSTracker(AndroidGPSTrackingActivity.this); // check if GPS enabled if(gps.canGetLocation()){ double latitude = gps.getLatitude(); double longitude = gps.getLongitude(); // \n is for new line Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show(); }else{ gps.showSettingsAlert(); } } });
¡He editado debajo del código pero no consigo cityname por favor ayúdeme !!!
Button btnShowLocation; GPSTracker gps; String CityName; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btnShowLocation = (Button) findViewById(R.id.btnShowLocation); // show location button click event btnShowLocation.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // create class object gps = new GPSTracker(AndroidGPSTrackingActivity.this); // check if GPS enabled if (gps.canGetLocation()) { double latitude = gps.getLatitude(); double longitude = gps.getLongitude(); Geocoder geocoder = new Geocoder( AndroidGPSTrackingActivity.this, Locale .getDefault()); List<Address> addresses; try { Log.v("log_tag", "latitude" + latitude); Log.v("log_tag", "longitude" + longitude); addresses = geocoder.getFromLocation(latitude, longitude, 1); Log.v("log_tag", "addresses+)_+++" + addresses); CityName = addresses.get(0).getAddressLine(0); Log.v("log_tag", "CityName" + CityName); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // \n is for new line Toast.makeText( getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG) .show(); } else { gps.showSettingsAlert(); } } });
Pero tengo un error en la parte inferior ::::
03-11 17:01:46.465: W/System.err(27587): java.io.IOException: Service not Available 03-11 17:01:46.465: W/System.err(27587): at android.location.Geocoder.getFromLocation(Geocoder.java:136) 03-11 17:01:46.465: W/System.err(27587): at com.example.gpstracking.AndroidGPSTrackingActivity$1.onClick(AndroidGPSTrackingActivity.java:81) 03-11 17:01:46.465: W/System.err(27587): at android.view.View.performClick(View.java:4191) 03-11 17:01:46.475: W/System.err(27587): at android.view.View$PerformClick.run(View.java:17229) 03-11 17:01:46.475: W/System.err(27587): at android.os.Handler.handleCallback(Handler.java:615) 03-11 17:01:46.475: W/System.err(27587): at android.os.Handler.dispatchMessage(Handler.java:92) 03-11 17:01:46.475: W/System.err(27587): at android.os.Looper.loop(Looper.java:137) 03-11 17:01:46.475: W/System.err(27587): at android.app.ActivityThread.main(ActivityThread.java:4960) 03-11 17:01:46.475: W/System.err(27587): at java.lang.reflect.Method.invokeNative(Native Method) 03-11 17:01:46.475: W/System.err(27587): at java.lang.reflect.Method.invoke(Method.java:511) 03-11 17:01:46.475: W/System.err(27587): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038) 03-11 17:01:46.475: W/System.err(27587): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) 03-11 17:01:46.475: W/System.err(27587): at dalvik.system.NativeStart.main(Native Method) 03-11 17:02:21.335: W/IInputConnectionWrapper(27587): getSelectedText on inactive InputConnection 03-11 17:02:21.335: W/IInputConnectionWrapper(27587): setComposingText on inactive InputConnection 03-11 17:02:21.425: W/IInputConnectionWrapper(27587): getExtractedText on inactive InputConnection
- Android GPS Localización Velocidad no fiable
- Cómo comprobar la ubicación de Mock en Android Marshmallow?
- ACCESS_COARSE_LOCATION es necesario si el usuario ya ha concedido ACCESS_FINE_LOCATION?
- Cómo mostrar el diálogo de configuración de ubicación en android?
- ¿Cómo encontrar si un punto GPS se encuentra en un conjunto determinado de inicio y fin ubicación GPS?
- ¿Android ProximityAlert o Geofence drenan más batería cuando el teléfono está más cerca de una ubicación supervisada?
- Android: ¿Una forma sencilla de hacer una geofence?
- Android Encuentra la latitud Longitud del punto X de la ubicación definida
Utilizar esta:
Geocoder geocoder = new Geocoder(this, Locale.getDefault()); List<Address> addresses = geocoder.getFromLocation(MyLat, MyLong, 1); String cityName = addresses.get(0).getAddressLine(0); String stateName = addresses.get(0).getAddressLine(1); String countryName = addresses.get(0).getAddressLine(2);
Para obtener más información en el ejemplo detallado de google map, consulta los siguientes enlaces: http://www.demoadda.com/demo/android/load-googlemap_107
Y para las actualizaciones de la ubicación de fondo: http://www.demoadda.com/demo/android/download-android-background-location-update-service-demo_21
En su onClick añadir esto:
Geocoder gcd = new Geocoder(AndroidGPSTrackingActivity.this, Locale.getDefault()); List<Address> addresses = gcd.getFromLocation(latitude, longitude, 1); if (addresses.size() > 0) System.out.println(addresses.get(0).getLocality());
gps = new LocationAddress(getActivity()); if (gps.canGetLocation()) { double latitude = gps.getLatitude(); double longitude = gps.getLongitude(); Geocoder geocoder = new Geocoder(getActivity(), Locale.getDefault()); //List<Address> addresses =geocoder.getFromLocation(latitude, longitude, 1); try { List < Address > addresses = geocoder.getFromLocation(latitude, longitude, 1); String address = addresses.get(0).getSubLocality(); String cityName = addresses.get(0).getLocality(); String stateName = addresses.get(0).getAdminArea(); txt_paddress.setText(address); txt_city.setText(cityName); txt_state.setText(stateName); } catch (IOException e) { e.printStackTrace(); } } else { gps.showSettingsAlert(); }
Utilice Geocoder. Pase la latitud y la longitud y obtenga el nombre y dirección de la ciudad.
try { Geocoder geo = new Geocoder(this.getApplicationContext(), Locale.getDefault()); List < Address > addresses = geo.getFromLocation(latitude, longitude, 1); if (addresses.isEmpty()) { addres.setText("Waiting for Location"); } else { if (addresses.size() > 0) { addres.setText(addresses.get(0).getFeatureName() + ", " + addresses.get(0).getLocality() + ", " + addresses.get(0).getAdminArea() + ", " + addresses.get(0).getCountryName()); //Toast.makeText(getApplicationContext(), "Address:- " + addresses.get(0).getFeatureName() + addresses.get(0).getAdminArea() + addresses.get(0).getLocality(), Toast.LENGTH_LONG).show(); } } } catch (Exception e) { e.printStackTrace(); // getFromLocation() may sometimes fail }
Para mi país (Vietnam) getLocality
devolverá null y addresses.get(0).getAddressLine(0)
devolverá el distrito no ciudad
Así que haré así
Geocoder geocoder = new Geocoder(this, Locale.getDefault()); List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1); int maxAddressLine = addresses.get(0).getMaxAddressLineIndex(); String countryName = addresses.get(0).getAddressLine(maxAddressLine); String stateName = addresses.get(0).getAddressLine(maxAddressLine-1); String cityName = addresses.get(0).getAddressLine(maxAddressLine-2);
try { Geocoder geocoder = new Geocoder(this, Locale.getDefault()); List<Address> addresses = geocoder.getFromLocation(28.5839624000, 77.3154441000, 1); String addres = addresses.get(0).getSubLocality(); String cityName = addresses.get(0).getLocality(); String stateName = addresses.get(0).getAdminArea(); String getPostalCode = addresses.get(0).getPostalCode(); Log.e("keshav","addres -> "+addres); Log.e("keshav","cityName -> "+ cityName); Log.e("keshav","stateName -> "+ stateName); Log.e("keshav","PostalCode -> "+ getPostalCode); } catch (IOException e) { e.printStackTrace(); }
Salida
Addres -> Sector 2
CityName -> Noida
StateName -> Uttar Pradesh
Código Postal -> 201301
- Error de Android aleatoriamente java.lang.NoClassDefFoundError: com.facebook.internal.Utility
- Estilo personalizado para Android TabWidget