In this blog,
I have shown how to fetch your current location using the older Apis. Google introduce the new API on the basis of the google play services.
For any location based application, this functionality is very use full to your user. So there are few steps to fetch your location in android.
How to fetch your location
Step 1: First all create a class which extends the Service class and implements the LocationListener,
123456789101112131415161718192021222324252627282930313233 public class MyGPSTracker extends Service implements LocationListener {public MyGPSTracker(Context context) {this.mContext = context;getLocation();}@Overridepublic void onLocationChanged(Location location) {}@Overridepublic void onStatusChanged(String provider, int status, Bundle extras) {}@Overridepublic void onProviderEnabled(String provider) {}@Overridepublic void onProviderDisabled(String provider) {}@Nullable@Overridepublic IBinder onBind(Intent intent) {return null;}}
Step 2: Then create the Location instance and try to get the latitude and longitude by the Location manager,
12345 Location location;protected LocationManager locationManager;double latitude; // latitudedouble longitude; // longitude
Create a method which returns the location instance with getting the lat-long,
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 public Location getLocation() {try {locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);// getting GPS statusisGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);// getting network statusisNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);if (!isGPSEnabled && !isNetworkEnabled) {// no network provider is enabled} else {this.canGetLocation = true;if (isNetworkEnabled) {locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, this);Log.d("Network", "Network");if (locationManager != null) {location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);if (location != null) {latitude = location.getLatitude();longitude = location.getLongitude();}}}// if GPS Enabled get lat/long using GPS Servicesif (isGPSEnabled) {if (location == null) {locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, this);Log.d("GPS Enabled", "GPS Enabled");if (locationManager != null) {location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);if (location != null) {latitude = location.getLatitude();longitude = location.getLongitude();}}}}}} catch (Exception e) {e.printStackTrace();}return location;}
Step 3: Create the function to getting the Latitude and Longitude,
1234567891011121314151617 public double getLatitude() {if (location != null) {latitude = location.getLatitude();}// return latitudereturn latitude;}public double getLongitude() {if (location != null) {longitude = location.getLongitude();}// return longitudereturn longitude;}
Step 4: Create another class from where to get the location,
1234567891011121314151617181920212223242526 public void getYourLocation(View v) {// permission check in Marshmallowif (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED &&ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)!= PackageManager.PERMISSION_GRANTED) {ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1);} else {gps = new MyGPSTracker(MyLocationClass.this);// check if GPS enabledif (gps.canGetLocation()) {latitude = gps.getLatitude();longitude = gps.getLongitude();Log.d("latitude",latitude + "");Log.d("longitude",longitude + "");} else {// can't get location// GPS or Network is not enabled// Ask user to enable GPS/network in settingsgps.showSettingsAlert();}}}
Step 5: After successfully getting the lat-long, we can get the user current location by Reverse Geocoding
12345678910111213 try {geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());addresses = geocoder.getFromLocation(latitude, longitude, 1);String address = addresses.get(0).getAddressLine(0);String city = addresses.get(0).getLocality();state = addresses.get(0).getAdminArea();country = addresses.get(0).getCountryName();String postalCode = addresses.get(0).getPostalCode();} catch (IOException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}Note: Reverse geocoding is the process of back (reverse) coding of a point location (latitude, longitude) to a readable address or place name. This permits the identification of nearby street addresses, places, and/or areal subdivisions such as neighbourhoods, county, state, or country.