-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLocationHelper.java
61 lines (44 loc) · 1.8 KB
/
LocationHelper.java
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
package com.arifulislam.tourguidepro;
import android.content.Context;
import android.location.Location;
import android.util.Log;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices;
public class LocationHelper {
private static Location currentLocation = null;
public static Location getLocation(Context context) {
FusedLocationProviderClient client = null;
LocationCallback callback = null;
LocationRequest locationRequest = null;
client = LocationServices.getFusedLocationProviderClient(context);
locationRequest = new LocationRequest()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(300)
.setFastestInterval(1000);
callback = new LocationCallback(){
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
for (Location location: locationResult.getLocations()) {
currentLocation = location;
}
}
};
try {
client.requestLocationUpdates(locationRequest, callback, null);
}catch (SecurityException e) {
e.printStackTrace();
}
Log.d("location", "getLocation: "+(currentLocation == null));
while(currentLocation == null) {
currentLocation = getLocation(context);
}
if (client != null && callback != null) {
client.removeLocationUpdates(callback);
}
return currentLocation;
}
}