@@ -16,6 +16,7 @@ import android.location.Location;
16
16
import android.location.LocationListener ;
17
17
import android.location.LocationManager ;
18
18
import android.os.Bundle ;
19
+ import android.Manifest ;
19
20
20
21
// Set up the variables for the LocationManager and LocationListener
21
22
LocationManager locationManager;
@@ -32,6 +33,8 @@ String[] fontList;
32
33
PFont androidFont;
33
34
Context context;
34
35
36
+ boolean hasLocation = false ;
37
+
35
38
void setup () {
36
39
size (480 , 640 , P2D );
37
40
// Sketch stays in portrait mode, even when the phone is rotated
@@ -42,36 +45,53 @@ void setup () {
42
45
textFont (androidFont);
43
46
}
44
47
45
- void draw ()
46
- {
48
+ void draw () {
47
49
background (0 );
48
- // Display current GPS data
49
- text (" Latitude: " + currentLatitude, 20 , 40 );
50
- text (" Longitude: " + currentLongitude, 20 , 75 );
51
- text (" Accuracy: " + currentAccuracy, 20 , 110 );
52
- text (" Provider: " + currentProvider, 20 , 145 );
50
+ if (hasLocation) {
51
+ // Display current GPS data
52
+ text (" Latitude: " + currentLatitude, 20 , 40 );
53
+ text (" Longitude: " + currentLongitude, 20 , 75 );
54
+ text (" Accuracy: " + currentAccuracy, 20 , 110 );
55
+ text (" Provider: " + currentProvider, 20 , 145 );
56
+ } else {
57
+ text (" No permissions to access location" , 20 , 40 );
58
+ }
59
+ }
60
+
61
+
62
+ void onPermissionsGranted () {
63
+ initLocation();
53
64
}
54
65
66
+ /*
55
67
// Override the activity class methods
56
68
void onResume() {
57
69
super.onResume();
58
-
59
- context = getActivity();
60
- // context = surface.getActivity();
61
- locationListener = new MyLocationListener ();
62
- locationManager = (LocationManager ) context. getSystemService(Context . LOCATION_SERVICE );
63
-
64
- // Register the listener with the Location Manager to receive location updates
65
- locationManager. requestLocationUpdates(LocationManager . NETWORK_PROVIDER , 0 , 0 , locationListener);
66
70
}
67
71
68
72
void onPause() {
69
73
super.onPause();
70
74
//locationManager.removeGpsStatusListener(locationListener);
71
75
}
76
+ */
72
77
73
78
/** ***************************************************************/
74
79
80
+ void initLocation () {
81
+ if (checkPermission(Manifest . permission. ACCESS_FINE_LOCATION ) ||
82
+ checkPermission(Manifest . permission. ACCESS_COARSE_LOCATION )) {
83
+ context = surface. getActivity();
84
+ locationListener = new MyLocationListener ();
85
+ locationManager = (LocationManager ) context. getSystemService(Context . LOCATION_SERVICE );
86
+
87
+ // Register the listener with the Location Manager to receive location updates
88
+ locationManager. requestLocationUpdates(LocationManager . NETWORK_PROVIDER , 0 , 0 , locationListener);
89
+ hasLocation = true ;
90
+ } else {
91
+ hasLocation = false ;
92
+ }
93
+ }
94
+
75
95
// Class for capturing the GPS data
76
96
class MyLocationListener implements LocationListener {
77
97
// Define all LocationListener methods
0 commit comments