Get location from GPS
Algorithm:
1.) Create a new project by File-> New -> Android Project name it GetLocationFromGPS.
2.) Write following into main.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" tools:context = ".MainActivity" > < TextView android:id = "@+id/status" android:layout_width = "fill_parent" android:layout_height = "wrap_content" /> < TextView android:id = "@+id/latitude" android:layout_width = "fill_parent" android:layout_height = "wrap_content" /> < TextView android:id = "@+id/longitude" android:layout_width = "fill_parent" android:layout_height = "wrap_content" /> </ LinearLayout > |
3.) Write following permissions into your manifest file:
1
2
| < uses-permission android:name = "android.permission.ACCESS_COARSE_LOCATION" /> < uses-permission android:name = "android.permission.ACCESS_FINE_LOCATION" /> |
4.) Run for output.
Steps:
1.) Create a project named GetLocationFromGPS and set the information as stated in the image.
Build Target: Android 4.4
Application Name: GetLocationFromGPS
Package Name: com.example.GetLocationFromGPS
Activity Name: GetLocationFromGPS
Application Name: GetLocationFromGPS
Package Name: com.example.GetLocationFromGPS
Activity Name: GetLocationFromGPS
2.) Open GetLocationFromGPS.java file and write following code there:
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
| package com.example.getlocationfromgps; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.widget.TextView; public class GetLocationFromGPS extends Activity { TextView testViewStatus, textViewLatitude, textViewLongitude; LocationManager myLocationManager; String PROVIDER = LocationManager.GPS_PROVIDER; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); testViewStatus = (TextView)findViewById(R.id.status); textViewLatitude = (TextView)findViewById(R.id.latitude); textViewLongitude = (TextView)findViewById(R.id.longitude); myLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); //get last known location, if available Location location = myLocationManager.getLastKnownLocation(PROVIDER); showMyLocation(location); } @Override protected void onPause() { // TODO Auto-generated method stub super .onPause(); myLocationManager.removeUpdates(myLocationListener); } @Override protected void onResume() { // TODO Auto-generated method stub super .onResume(); myLocationManager.requestLocationUpdates( PROVIDER, //provider 0 , //minTime 0 , //minDistance myLocationListener); //LocationListener } private void showMyLocation(Location l){ if (l == null ){ testViewStatus.setText( "No Location!" ); } else { textViewLatitude.setText( "Latitude: " + l.getLatitude()); textViewLongitude.setText( "Longitude: " + l.getLongitude()); } } private LocationListener myLocationListener = new LocationListener(){ @Override public void onLocationChanged(Location location) { showMyLocation(location); } @Override public void onProviderDisabled(String provider) { // TODO Auto-generated method stub } @Override public void onProviderEnabled(String provider) { // TODO Auto-generated method stub } @Override public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub }}; } |
3.) Compile and build the project.
Output