Many people are curious about how GPS works and wants to make some App. using the GPS coordinates. GPS stands for Global Positioning System and provides you the geographical coordinates of your current location. These geographical coordinates are known as Latitude and Longitude. To know more about Latitude and Longitude please visit “Understanding GPS coordinates Latitude and Longitute

Here we are going to create an Android App. using the Android location manager to get the current geographical coordinates(Latitude and Longitude)

Step 1. Add the user permission to AndroidManifest.xml file:-
Add this:-

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

before the <appliaction> tag

Step 2. Now we will create a class which will get the Latitude and Longitude of the current location.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
public class getLocation extends Service implements LocationListener {
 
	private final Context mContext;
 
	// flag for GPS status
	boolean isGPSActive  = 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 = 1000 * 60 * 1; // 1 minute
 
	// Declaring a Location Manager
	protected LocationManager locationManager;
 
	public getLocation(Context context) {
		this.mContext = context;
		getTheLocation();
	}
 
	public Location getTheLocation() {
		try {
			locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
 
			// getting GPS status
			isGPSActive  = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
 
			if (!isGPSActive ) {
				// GPS Not Active
			} else {				
				if (location == null) {
					locationManager.requestLocationUpdates(
							LocationManager.GPS_PROVIDER,
							MIN_TIME_BW_UPDATES,
							MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
					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 isGPSActive() {
		return this.isGPSActive;
	}
 
	@Override
	public void onLocationChanged(Location location) {
	}
 
	@Override
	public void onProviderDisabled(String provider) {
	}
 
	@Override
	public void onProviderEnabled(String provider) {
	}
 
	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
	}
 
	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}
}

Step 3:- Get the location and display Latitude and Longitude using the “getLocation” class we created above:-

1
2
3
4
5
6
7
8
9
getLocation gl = new getLocation(MainActivity.this);
// check if GPS enabled		
if(gl.isGPSActive()){	
	double lat = gl.getLatitude();
	double long = gl.getLongitude();	
	Toast.makeText(getApplicationContext(), "Latitude: " + lat + " And Longitude: " + long, Toast.LENGTH_LONG).show();	
}else{
	// GPS is not enabled.
}