You can see other best Android Gists or offer your just here https://github.com/lopspower/BestAndroidGists 👍.
-
-
Save lopspower/9524cd7028fa5d813414 to your computer and use it in GitHub Desktop.
Utils Class to get localisation information in Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.Manifest; | |
import android.content.Context; | |
import android.location.Address; | |
import android.location.Geocoder; | |
import android.location.Location; | |
import android.location.LocationListener; | |
import android.location.LocationManager; | |
import android.support.annotation.RequiresPermission; | |
import java.io.IOException; | |
import java.util.List; | |
/** | |
* Copyright (C) 2016 Mikhael LOPEZ | |
* Licensed under the Apache License Version 2.0 | |
* Utility class for the localisation management | |
*/ | |
public class LocalisationUtils { | |
//region Public Method | |
public static String getCurrentAddress(Context context) { | |
List<Address> addresses = getListAddressFromGeocoder(context); | |
return (addresses != null) ? addresses.get(0).getAddressLine(0) : null; | |
} | |
public static String getCurrentCity(Context context) { | |
List<Address> addresses = getListAddressFromGeocoder(context); | |
return (addresses != null) ? addresses.get(0).getLocality() : null; | |
} | |
public static String getCurrentCountry(Context context) { | |
List<Address> addresses = getListAddressFromGeocoder(context); | |
return (addresses != null) ? addresses.get(0).getCountryName() : null; | |
} | |
//endregion | |
//region Private Method | |
@RequiresPermission(allOf = {Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}) | |
private static Location getLocation(Context context, LocationListener locationListener) throws IOException { | |
// Use the LocationManager class to obtain GPS locations | |
LocationManager mlocManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); | |
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 300000, 40, locationListener); | |
Location location = mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); | |
// Check both providers even for lastKnownLocation | |
if (location == null) { | |
location = mlocManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); | |
} | |
return location; | |
} | |
private static List<Address> getListAddressFromGeocoder(Context context) { | |
List<Address> addresses = null; | |
try { | |
Location location = getLocation(context, new LocationListener() { | |
@Override | |
public void onLocationChanged(Location location) {} | |
@Override | |
public void onStatusChanged(String provider, int status, Bundle extras) {} | |
@Override | |
public void onProviderEnabled(String provider) {} | |
@Override | |
public void onProviderDisabled(String provider) {} | |
}); | |
if (location != null) { | |
double latitude = location.getLatitude(); | |
double longitude = location.getLongitude(); | |
if (ConnectionUtils.isConnected(context)) { | |
Geocoder geocoder = new Geocoder(context); | |
addresses = geocoder.getFromLocation(latitude, longitude, 1); | |
} | |
} | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} | |
return addresses; | |
} | |
//endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment