Skip to content
This repository was archived by the owner on Jun 24, 2024. It is now read-only.

Commit 4aeef0d

Browse files
committed
gps example uses new permission functions
1 parent e87012d commit 4aeef0d

File tree

1 file changed

+35
-15
lines changed

1 file changed

+35
-15
lines changed

location_permissions/gps_example/gps_example.pde

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import android.location.Location;
1616
import android.location.LocationListener;
1717
import android.location.LocationManager;
1818
import android.os.Bundle;
19+
import android.Manifest;
1920

2021
// Set up the variables for the LocationManager and LocationListener
2122
LocationManager locationManager;
@@ -32,6 +33,8 @@ String[] fontList;
3233
PFont androidFont;
3334
Context context;
3435

36+
boolean hasLocation = false;
37+
3538
void setup () {
3639
size(480, 640, P2D);
3740
// Sketch stays in portrait mode, even when the phone is rotated
@@ -42,36 +45,53 @@ void setup () {
4245
textFont(androidFont);
4346
}
4447

45-
void draw()
46-
{
48+
void draw() {
4749
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();
5364
}
5465

66+
/*
5567
// Override the activity class methods
5668
void onResume() {
5769
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);
6670
}
6771
6872
void onPause() {
6973
super.onPause();
7074
//locationManager.removeGpsStatusListener(locationListener);
7175
}
76+
*/
7277

7378
/*****************************************************************/
7479

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+
7595
// Class for capturing the GPS data
7696
class MyLocationListener implements LocationListener {
7797
// Define all LocationListener methods

0 commit comments

Comments
 (0)