Java Code Examples for com.google.android.gms.location.LocationServices#getFusedLocationProviderClient()

The following examples show how to use com.google.android.gms.location.LocationServices#getFusedLocationProviderClient() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WhereAmIActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 6 votes vote down vote up
private void getLastLocation() {
  FusedLocationProviderClient fusedLocationClient;
  fusedLocationClient =
    LocationServices.getFusedLocationProviderClient(this);
  if (ActivityCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION)
      == PERMISSION_GRANTED ||
      ActivityCompat.checkSelfPermission(this, ACCESS_COARSE_LOCATION)
      == PERMISSION_GRANTED) {
    fusedLocationClient.getLastLocation()
      .addOnSuccessListener(this, new OnSuccessListener<Location>() {
        @Override
        public void onSuccess(Location location) {
          updateTextView(location);
        }
      });
  }
}
 
Example 2
Source File: WhereAmIActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 6 votes vote down vote up
private void getLastLocation() {
  FusedLocationProviderClient fusedLocationClient;
  fusedLocationClient =
    LocationServices.getFusedLocationProviderClient(this);
  if (ActivityCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION)
      == PERMISSION_GRANTED ||
      ActivityCompat.checkSelfPermission(this, ACCESS_COARSE_LOCATION)
      == PERMISSION_GRANTED) {
    fusedLocationClient.getLastLocation()
      .addOnSuccessListener(this, new OnSuccessListener<Location>() {
        @Override
        public void onSuccess(Location location) {
          updateTextView(location);
        }
      });
  }
}
 
Example 3
Source File: LocationActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 6 votes vote down vote up
private void startTrackingLocation() {
  if (
    ActivityCompat
      .checkSelfPermission(this, ACCESS_FINE_LOCATION) == PERMISSION_GRANTED ||
      ActivityCompat
        .checkSelfPermission(this, ACCESS_COARSE_LOCATION) == PERMISSION_GRANTED) {

    FusedLocationProviderClient locationClient = LocationServices.getFusedLocationProviderClient(this);
    LocationRequest request =
      new LocationRequest()
        .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
        .setInterval(5000); // Update every 5 seconds.

    locationClient.requestLocationUpdates(request, mLocationCallback, null);
  }
}
 
Example 4
Source File: BasePlatformCreate.java    From indigenous-android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Initiate the location libraries and services.
 */
private void initLocationLibraries() {
    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
    mSettingsClient = LocationServices.getSettingsClient(this);

    mLocationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {
            super.onLocationResult(locationResult);
            // location is received
            mCurrentLocation = locationResult.getLastLocation();
            updateLocationUI();
        }
    };

    mRequestingLocationUpdates = false;

    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
    mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
    builder.addLocationRequest(mLocationRequest);
    mLocationSettingsRequest = builder.build();
}
 
Example 5
Source File: PlaceSearchFragment.java    From Place-Search-Service with MIT License 6 votes vote down vote up
@Override
public void onAttach(Context context) {
    super.onAttach(context);
    dialog = new ProgressDialog(context);
    dialog.setCancelable(false);
    dialog.setIndeterminate(true);
    dialog.setMessage("Fetching Results");
    dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    this.context = context;
    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(context);
    mSettingsClient = LocationServices.getSettingsClient(context);
    adapter = new CustomAutoCompleteAdapter(context);
    createLocationCallback();
    createLocationRequest();
    buildLocationSettingsRequest();
}
 
Example 6
Source File: LocationGetLocationActivity.java    From coursera-android with MIT License 6 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    mAccuracyView = findViewById(R.id.accuracy_view);
    mTimeView = findViewById(R.id.time_view);
    mLatView = findViewById(R.id.lat_view);
    mLngView = findViewById(R.id.lng_view);

    mLocationClient = LocationServices.getFusedLocationProviderClient(this);

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) !=
            PackageManager.PERMISSION_GRANTED) {
        requestPermissions(new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, FINE_LOC_PERM_REQ);
    } else {
        getLastKnownLocation();
    }

}
 
Example 7
Source File: MainActivity.java    From location-samples with Apache License 2.0 6 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    mResultReceiver = new AddressResultReceiver(new Handler());

    mLocationAddressTextView = (TextView) findViewById(R.id.location_address_view);
    mProgressBar = (ProgressBar) findViewById(R.id.progress_bar);
    mFetchAddressButton = (Button) findViewById(R.id.fetch_address_button);

    // Set defaults, then update using values stored in the Bundle.
    mAddressRequested = false;
    mAddressOutput = "";
    updateValuesFromBundle(savedInstanceState);

    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

    updateUIWidgets();
}
 
Example 8
Source File: LocationActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 6 votes vote down vote up
private void listing15_5() {
  int permission = ActivityCompat.checkSelfPermission(this,
    ACCESS_FINE_LOCATION);
  if (permission == PERMISSION_GRANTED) {

    // LISTING 15-5: Obtaining the last known device Location
    FusedLocationProviderClient fusedLocationClient;
    fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
    fusedLocationClient.getLastLocation()
      .addOnSuccessListener(this, new OnSuccessListener<Location>() {
        @Override
        public void onSuccess(Location location) {
          // In some rare situations this can be null.
          if (location != null) {
            // TODO Do something with the returned location.
          }
        }
      });
  }
}
 
Example 9
Source File: GMSLocationService.java    From GeometricWeather with GNU Lesser General Public License v3.0 5 votes vote down vote up
public GMSLocationService(Context c) {
    context = c;
    timer = new Handler(Looper.getMainLooper());

    client = LocationServices.getFusedLocationProviderClient(context);
    locationListener = null;
    locationCallback = null;
    lastKnownLocation = null;
}
 
Example 10
Source File: WhereAmIActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
private void requestLocationUpdates() {
  if (ActivityCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION)
        == PERMISSION_GRANTED ||
        ActivityCompat.checkSelfPermission(this, ACCESS_COARSE_LOCATION)
        == PERMISSION_GRANTED) {

    FusedLocationProviderClient fusedLocationClient
      = LocationServices.getFusedLocationProviderClient(this);

    fusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, null);
  }
}
 
Example 11
Source File: LocationService.java    From io.appium.settings with Apache License 2.0 5 votes vote down vote up
private FusedLocationProvider createFusedLocationProvider() {
    GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .build();
    FusedLocationProviderClient locationProviderClient = LocationServices.getFusedLocationProviderClient(this);
    return new FusedLocationProvider(googleApiClient, locationProviderClient, this);
}
 
Example 12
Source File: WhereAmIActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
private void requestLocationUpdates() {
  if (ActivityCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION)
        == PERMISSION_GRANTED ||
        ActivityCompat.checkSelfPermission(this, ACCESS_COARSE_LOCATION)
        == PERMISSION_GRANTED) {

    FusedLocationProviderClient fusedLocationClient
      = LocationServices.getFusedLocationProviderClient(this);

    fusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, null);
  }
}
 
Example 13
Source File: ScannerService.java    From com.ruuvi.station with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void updateLocation() {
    FusedLocationProviderClient mFusedLocationClient = LocationServices.getFusedLocationProviderClient(getApplicationContext());
    if (ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        mFusedLocationClient.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
            @Override
            public void onSuccess(Location location) {
                tagLocation = location;
            }
        });
    }
}
 
Example 14
Source File: WhereAmIActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
private void requestLocationUpdates() {
  if (ActivityCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION)
        == PERMISSION_GRANTED ||
        ActivityCompat.checkSelfPermission(this, ACCESS_COARSE_LOCATION)
        == PERMISSION_GRANTED) {

    FusedLocationProviderClient fusedLocationClient
      = LocationServices.getFusedLocationProviderClient(this);

    fusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, null);
  }
}
 
Example 15
Source File: LocationBackgroundService.java    From rn-background-location with MIT License 5 votes vote down vote up
@SuppressLint("MissingPermission")
@Override
protected void onHandleIntent(@Nullable Intent intent) {
    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(getApplicationContext());
    mLocationCallback = createLocationRequestCallback();

    LocationRequest locationRequest = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setInterval(0)
            .setFastestInterval(0);

    new Handler(getMainLooper()).post(() -> mFusedLocationClient.requestLocationUpdates(locationRequest, mLocationCallback, null));
}
 
Example 16
Source File: CoordinatesView.java    From dhis2-android-capture-app with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void init(Context context) {
    super.init(context);
    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(getContext());
}
 
Example 17
Source File: LocationUpdateWorker.java    From PixelWatchFace with GNU General Public License v3.0 4 votes vote down vote up
@NonNull
@Override
public ListenableFuture<Result> startWork() {
  String TAG = "location_update_worker";
  Log.d(TAG, "starting location work...");
  FusedLocationProviderClient fusedLocationClient = LocationServices
      .getFusedLocationProviderClient(getApplicationContext());

  return CallbackToFutureAdapter.getFuture(completer -> {
    if (ActivityCompat
        .checkSelfPermission(getApplicationContext(), permission.ACCESS_FINE_LOCATION)
        != PackageManager.PERMISSION_GRANTED
        && ActivityCompat
        .checkSelfPermission(getApplicationContext(), permission.ACCESS_COARSE_LOCATION)
        != PackageManager.PERMISSION_GRANTED) {
      completer.set(Result.failure());
    } else {
      fusedLocationClient.getLastLocation().addOnSuccessListener(location -> {
        if (location != null) {
          Log.d(TAG, "location: (" + location.getLatitude() + "," + location
              .getLongitude() + ")");
          //mCurrentWeather.setLocation(location);
          //Log.d(TAG, "gson-ized location: " + new Gson().toJson(location));
          Data output = new Data.Builder()
              .putDouble(KEY_LATITUDE, location.getLatitude())
              .putDouble(KEY_LONGITUDE, location.getLongitude())
              .putDouble(KEY_ALTITUDE, location.getAltitude())
              .putString(KEY_LOCATION_PROVIDER, location.getProvider())
              .build();

          completer.set(Result.success(output));
        } else {
          if (ActivityCompat.checkSelfPermission(getApplicationContext(),
              Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            completer.set(Result.failure());
          } else {
            // if no location, but permission exists, try again
            Log.d(TAG, "error retrieving location");
            completer.set(Result.retry());
          }
        }
      });
    }
    return completer;
  });
}
 
Example 18
Source File: LocationProvider.java    From LocationAware with Apache License 2.0 4 votes vote down vote up
public LocationProvider(Context context) {
  this.context = context;
  locationProviderClient = LocationServices.getFusedLocationProviderClient(context);
}
 
Example 19
Source File: RuuviRangeNotifier.java    From com.ruuvi.station with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public RuuviRangeNotifier(Context context, String from) {
    Log.d(TAG, "Setting up range notifier from " + from);
    this.context = context;
    this.from = from;
    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(context);
}
 
Example 20
Source File: GeofencesScanner.java    From PhoneProfilesPlus with Apache License 2.0 4 votes vote down vote up
@Override
public void onConnected(Bundle bundle) {
    //PPApplication.logE("##### GeofenceScanner.onConnected", "xxx");
    try {
        int version = GoogleApiAvailability.getInstance().getApkVersion(this.context);
        PPApplication.setCustomKey(PPApplication.CRASHLYTICS_LOG_GOOGLE_PLAY_SERVICES_VERSION, version);
    } catch (Exception e) {
        // https://github.com/firebase/firebase-android-sdk/issues/1226
        //PPApplication.recordException(e);
    }
    /*if (PPApplication.logEnabled()) {
        if (PhoneProfilesService.getInstance() != null)
            PPApplication.logE("##### GeofenceScanner.onConnected", "PhoneProfilesService.isGeofenceScannerStarted()=" + PhoneProfilesService.getInstance().isGeofenceScannerStarted());
    }*/
    try {
        if ((mGoogleApiClient != null) && mGoogleApiClient.isConnected()) {
            //PPApplication.logE("##### GeofenceScanner.onConnected", "xxx2");
            mFusedLocationClient = LocationServices.getFusedLocationProviderClient(context);
            useGPS = true;

            PPApplication.startHandlerThread(/*"GeofenceScanner.onConnected"*/);
            final Handler handler6 = new Handler(PPApplication.handlerThread.getLooper());
            handler6.post(new Runnable() {
                @Override
                public void run() {
                    PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
                    PowerManager.WakeLock wakeLock = null;
                    try {
                        if (powerManager != null) {
                            wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, PPApplication.PACKAGE_NAME + ":GeofenceScanner_onConnected");
                            wakeLock.acquire(10 * 60 * 1000);
                        }

                        //PPApplication.logE("PPApplication.startHandlerThread", "START run - from=GeofenceScanner.onConnected");

                        if ((PhoneProfilesService.getInstance() != null) && PhoneProfilesService.getInstance().isGeofenceScannerStarted()) {
                            GeofencesScanner scanner = PhoneProfilesService.getInstance().getGeofencesScanner();
                            if (scanner != null) {
                                scanner.clearAllEventGeofences();
                                //PPApplication.logE("##### GeofenceScanner.onConnected", "updateTransitionsByLastKnownLocation");
                                scanner.startLocationUpdates();
                                scanner.updateTransitionsByLastKnownLocation(false);
                            }
                        }

                        //PPApplication.logE("PPApplication.startHandlerThread", "END run - from=GeofenceScanner.onConnected");
                    } finally {
                        if ((wakeLock != null) && wakeLock.isHeld()) {
                            try {
                                wakeLock.release();
                            } catch (Exception ignored) {}
                        }
                    }
                }
            });
        }
    } catch (Exception ee) {
        //Log.e("##### GeofenceScanner.onConnected", Log.getStackTraceString(e));
        PPApplication.recordException(ee);
    }
}