Search in sources :

Example 1 with LocationSettingsRequest

use of com.google.android.gms.location.LocationSettingsRequest in project IITB-App by wncc.

the class MapFragment method setupGPS.

public void setupGPS(boolean showWarning) {
    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 {
            // Create the location request to start receiving updates
            mLocationRequest = new LocationRequest();
            mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
            mLocationRequest.setInterval(500);
            mLocationRequest.setFastestInterval(200);
            // Create LocationSettingsRequest object using location request
            LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
            builder.addLocationRequest(mLocationRequest);
            LocationSettingsRequest locationSettingsRequest = builder.build();
            // Check whether location settings are satisfied
            SettingsClient settingsClient = LocationServices.getSettingsClient(getActivity());
            settingsClient.checkLocationSettings(locationSettingsRequest);
            // Setup the callback
            myLocationCallback = new MyLocationCallback();
            fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(getActivity());
            fusedLocationProviderClient.requestLocationUpdates(mLocationRequest, myLocationCallback, Looper.myLooper());
            GPSIsSetup = true;
            if (showWarning) {
                Toast.makeText(getContext(), "WARNING: Location is in Beta. Use with Caution.", Toast.LENGTH_LONG).show();
            }
        } catch (SecurityException ignored) {
            Toast.makeText(getContext(), "No permission!", Toast.LENGTH_LONG).show();
        }
    }
}
Also used : SettingsClient(com.google.android.gms.location.SettingsClient) LocationRequest(com.google.android.gms.location.LocationRequest) LocationSettingsRequest(com.google.android.gms.location.LocationSettingsRequest) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 2 with LocationSettingsRequest

use of com.google.android.gms.location.LocationSettingsRequest in project wiseasily by eFishery.

the class MainActivity method initLocationParameters.

private void initLocationParameters() {
    LocationRequest mLocationRequest = new LocationRequest();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_LOW_POWER);
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
    builder.addLocationRequest(mLocationRequest);
    LocationSettingsRequest locationSettingsRequest = builder.build();
    Task<LocationSettingsResponse> result = LocationServices.getSettingsClient(this).checkLocationSettings(locationSettingsRequest);
    result.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {

        @Override
        public void onComplete(@NonNull Task<LocationSettingsResponse> task) {
            try {
                LocationSettingsResponse response = task.getResult(ApiException.class);
                tryToConnect();
            } catch (ApiException exception) {
                switch(exception.getStatusCode()) {
                    case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                        try {
                            ResolvableApiException resolvable = (ResolvableApiException) exception;
                            resolvable.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
                        } catch (IntentSender.SendIntentException e) {
                            Log.d("MainActivity_Location", e.toString());
                        } catch (ClassCastException e) {
                            Log.d("MainActivity_Location", e.toString());
                        }
                        break;
                    case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                        Log.d("MainActivity_Location", "LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE");
                        break;
                }
            }
        }
    });
}
Also used : ResolvableApiException(com.google.android.gms.common.api.ResolvableApiException) LocationRequest(com.google.android.gms.location.LocationRequest) LocationSettingsResponse(com.google.android.gms.location.LocationSettingsResponse) LocationSettingsRequest(com.google.android.gms.location.LocationSettingsRequest) IntentSender(android.content.IntentSender) ResolvableApiException(com.google.android.gms.common.api.ResolvableApiException) ApiException(com.google.android.gms.common.api.ApiException)

Aggregations

LocationRequest (com.google.android.gms.location.LocationRequest)2 LocationSettingsRequest (com.google.android.gms.location.LocationSettingsRequest)2 IntentSender (android.content.IntentSender)1 SpannableStringBuilder (android.text.SpannableStringBuilder)1 ApiException (com.google.android.gms.common.api.ApiException)1 ResolvableApiException (com.google.android.gms.common.api.ResolvableApiException)1 LocationSettingsResponse (com.google.android.gms.location.LocationSettingsResponse)1 SettingsClient (com.google.android.gms.location.SettingsClient)1