Skip to main content

Get Current Location using GPS/Network Provider in Android

Hello to all,

 Today I will teach you that how can we get current location.

 In android’s new SDK tools, LocationManager class does not give any network provider’s status. It always gives null value. Still it works in some of the android devices rarely. So that’s why I include code for both network provider and gps provider for getting current location.

 Create one service class name LocationFinder and implements it LocationListener.

 All the code which is related to Location, is doing in that service class and from MainActivity, call its method through its instance in onResume() method.

 The reason you know, why its called in onResume(), because when you enable GPS and come back in activity, then its again check Location’s status.

 In the code, if GPS not enable, I show settings alert pop-up, to go settings menu. When you enable GPS, you will get current location after 2–3 seconds. If not, come again to activity and surely get location.

 Make sure, must add persmissions:
<uses-permission
android:name=”android.permission.ACCESS_COARSE_LOCATION” />
<uses-permission
android:name=”android.permission.ACCESS_FINE_LOCATION”/> 
LocationFinder.java
package com.droidbyme.currentlocation;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
public class LocationFinder extends Service implements LocationListener {
Context context;
boolean isGPSEnabled = false;
// flag for network status
 boolean isNetworkEnabled = false;
// flag for GPS status
 boolean canGetLocation = false;
Location location; // location
 double latitude; // latitude
 double longitude; // longitude
// The minimum distance to change Updates in meters
 private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
// The minimum time between updates in milliseconds
 private static final long MIN_TIME_BW_UPDATES = 200 * 10 * 1; // 2 seconds
// Declaring a Location Manager
 protected LocationManager locationManager;
public LocationFinder(Context context) {
 this.context = context;
 getLocation();
 }
@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) {
}
@Nullable
 @Override
 public IBinder onBind(Intent intent) {
 return null;
 }
public Location getLocation() {
 try {
 locationManager = (LocationManager) context
 .getSystemService(LOCATION_SERVICE);
// getting GPS status
 isGPSEnabled = locationManager
 .isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
 isNetworkEnabled = locationManager
 .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
 // no network provider is enabled
 // Log.e(“Network-GPS”, “Disable”);
 } else {
 this.canGetLocation = true;
 // First get location from Network Provider
 if (isNetworkEnabled) {
 locationManager.requestLocationUpdates(
 LocationManager.NETWORK_PROVIDER,
 MIN_TIME_BW_UPDATES,
 MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
 // Log.e(“Network”, “Network”);
 if (locationManager != null) {
 location = locationManager
 .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
 if (location != null) {
 latitude = location.getLatitude();
 longitude = location.getLongitude();
 }
 }
 } else
 // if GPS Enabled get lat/long using GPS Services
 if (isGPSEnabled) {
 if (location == null) {
 locationManager.requestLocationUpdates(
 LocationManager.GPS_PROVIDER,
 MIN_TIME_BW_UPDATES,
 MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
 //Log.e(“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;
 }
public double getLatitude() {
 if (location != null) {
 latitude = location.getLatitude();
 }
 return latitude;
 }
public double getLongitude() {
 if (location != null) {
 longitude = location.getLongitude();
 }
 return longitude;
 }
public boolean canGetLocation() {
 return this.canGetLocation;
 }
public void showSettingsAlert() {
 AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
 alertDialog.setTitle(“GPS settings”);
 alertDialog.setMessage(“GPS is not enabled. Do you want to go to settings menu?”);
alertDialog.setPositiveButton(“Settings”, new DialogInterface.OnClickListener() {
 public void onClick(DialogInterface dialog, int which) {
 Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
 context.startActivity(intent);
 }
 });
alertDialog.setNegativeButton(“Cancel”, new DialogInterface.OnClickListener() {
 public void onClick(DialogInterface dialog, int which) {
 dialog.cancel();
 }
 });
alertDialog.show();
 }
} 
Do as follow where you want to get location:
LocationFinder finder;
double longitude = 0.0, latitude = 0.0;
finder = new LocationFinder(this);
if (finder.canGetLocation()) {
 latitude = finder.getLatitude();
 longitude = finder.getLongitude();
 Toast.makeText(this, “lat-lng “ + latitude + “ — “ + longitude, Toast.LENGTH_LONG).show();
} else {
 finder.showSettingsAlert();
} 

Comments