Skip to content

Instantly share code, notes, and snippets.

@lopspower
Last active August 4, 2016 06:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lopspower/9524cd7028fa5d813414 to your computer and use it in GitHub Desktop.
Save lopspower/9524cd7028fa5d813414 to your computer and use it in GitHub Desktop.
Utils Class to get localisation information in Android
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