use of com.google.android.gms.location.FusedLocationProviderClient in project Walrus by megabug.
the class ProjectWalrusApplication method startLocationUpdates.
public static void startLocationUpdates() {
FusedLocationProviderClient fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(context);
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setInterval(2000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
try {
fusedLocationProviderClient.requestLocationUpdates(locationRequest, new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
for (Location location : locationResult.getLocations()) {
if (currentBestLocation == null || GeoUtils.isBetterLocation(location, currentBestLocation))
currentBestLocation = location;
}
}
}, null);
} catch (SecurityException ignored) {
}
}
use of com.google.android.gms.location.FusedLocationProviderClient in project IITB-App by wncc.
the class FileComplaintFragment method getMapReady.
public void getMapReady() {
mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap map) {
googleMap = map;
UiSettings uiSettings = googleMap.getUiSettings();
uiSettings.setAllGesturesEnabled(true);
uiSettings.setZoomControlsEnabled(true);
uiSettings.setMyLocationButtonEnabled(true);
uiSettings.setIndoorLevelPickerEnabled(true);
uiSettings.setScrollGesturesEnabled(true);
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getActivity(), new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, MY_PERMISSIONS_REQUEST_LOCATION);
} else {
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() {
@Override
public boolean onMyLocationButtonClick() {
locate();
return false;
}
});
FusedLocationProviderClient mFusedLocationClient = LocationServices.getFusedLocationProviderClient(getActivity());
mFusedLocationClient.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
// Got last known location. In some rare situations this can be null.
if (location != null) {
// Logic to handle location object
Location = new LatLng(location.getLatitude(), location.getLongitude());
updateMap(Location, "Current Location", location.getLatitude() + ", " + location.getLongitude(), cursor);
} else {
Toast.makeText(getContext(), getString(R.string.getting_current_location), Toast.LENGTH_SHORT).show();
}
}
});
mFusedLocationClient.getLastLocation().addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
e.printStackTrace();
}
});
}
}
});
}
use of com.google.android.gms.location.FusedLocationProviderClient in project PhoneProfilesPlus by henrichg.
the class GeofencesScanner method updateTransitionsByLastKnownLocation.
/*
void resetLocationUpdates(boolean forScreenOn) {
stopLocationUpdates();
createLocationRequest();
PPApplication.logE("GeofenceScanner.scheduleJob", "from GeofenceScanner.resetLocationUpdates");
// startLocationUpdates is called from GeofenceScannerJob
if (PhoneProfilesService.instance != null)
PhoneProfilesService.instance.scheduleGeofenceScannerJob(true, false, true, forScreenOn, true);
}
*/
@SuppressLint("MissingPermission")
void updateTransitionsByLastKnownLocation(final boolean startEventsHandler) {
try {
if (Permissions.checkLocation(context) && (mGoogleApiClient != null) && mGoogleApiClient.isConnected()) {
PPApplication.logE("##### GeofenceScanner.updateTransitionsByLastKnownLocation", "xxx");
FusedLocationProviderClient fusedLocationClient = LocationServices.getFusedLocationProviderClient(context);
final Context appContext = context.getApplicationContext();
// noinspection MissingPermission
fusedLocationClient.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
// Got last known location. In some rare situations this can be null.
PPApplication.logE("##### GeofenceScanner.updateTransitionsByLastKnownLocation", "onSuccess");
if (location != null) {
PPApplication.logE("##### GeofenceScanner.updateTransitionsByLastKnownLocation", "location=" + location);
synchronized (PPApplication.geofenceScannerLastLocationMutex) {
lastLocation.set(location);
}
PPApplication.startHandlerThread();
final Handler handler = new Handler(PPApplication.handlerThread.getLooper());
handler.post(new Runnable() {
@Override
public void run() {
PowerManager powerManager = (PowerManager) appContext.getSystemService(POWER_SERVICE);
PowerManager.WakeLock wakeLock = null;
if (powerManager != null) {
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "GeofenceScanner.updateTransitionsByLastKnownLocation");
wakeLock.acquire(10 * 60 * 1000);
}
updateGeofencesInDB();
if (startEventsHandler) {
// start job
// EventsHandlerJob.startForSensor(appContext, EventsHandler.SENSOR_TYPE_LOCATION_MODE);
EventsHandler eventsHandler = new EventsHandler(appContext);
eventsHandler.handleEvents(EventsHandler.SENSOR_TYPE_LOCATION_MODE);
}
if ((wakeLock != null) && wakeLock.isHeld())
wakeLock.release();
}
});
}
}
});
}
} catch (Exception ignored) {
}
}
use of com.google.android.gms.location.FusedLocationProviderClient in project IITB-App by wncc.
the class FileComplaintFragment method locate.
private void locate() {
if (!GPSIsSetup) {
displayLocationSettingsRequest();
} else {
try {
FusedLocationProviderClient mFusedLocationClient = LocationServices.getFusedLocationProviderClient(getActivity());
mFusedLocationClient.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
// Got last known location. In some rare situations this can be null.
if (location != null) {
// Logic to handle location object
Location = new LatLng(location.getLatitude(), location.getLongitude());
updateMap(Location, "Current Location", location.getLatitude() + ", " + location.getLongitude(), cursor);
} else {
Toast.makeText(getContext(), getString(R.string.getting_current_location), Toast.LENGTH_SHORT).show();
}
}
});
mFusedLocationClient.getLastLocation().addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
e.printStackTrace();
Toast.makeText(getContext(), "Something went wrong while getting your location \n" + e, Toast.LENGTH_LONG).show();
}
});
GPSIsSetup = true;
} catch (SecurityException ignored) {
Toast.makeText(getContext(), getString(R.string.no_permission), Toast.LENGTH_LONG).show();
}
}
}
use of com.google.android.gms.location.FusedLocationProviderClient in project IITB-App by wncc.
the class FileComplaintFragment method setupGPS.
private void setupGPS() {
if (getView() == null || getActivity() == null)
return;
// Permissions stuff
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getActivity(), new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, MY_PERMISSIONS_REQUEST_LOCATION);
} else {
try {
FusedLocationProviderClient mFusedLocationClient = LocationServices.getFusedLocationProviderClient(getActivity());
mFusedLocationClient.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
// Got last known location. In some rare situations this can be null.
if (location != null) {
// Logic to handle location object
Location = new LatLng(location.getLatitude(), location.getLongitude());
updateMap(Location, "Current Location", location.getLatitude() + ", " + location.getLongitude(), cursor);
} else {
Toast.makeText(getContext(), getString(R.string.getting_current_location), Toast.LENGTH_SHORT).show();
}
}
});
mFusedLocationClient.getLastLocation().addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
e.printStackTrace();
}
});
GPSIsSetup = true;
} catch (SecurityException ignored) {
Toast.makeText(getContext(), getString(R.string.no_permission), Toast.LENGTH_LONG).show();
}
}
}
Aggregations