Java Code Examples for com.google.android.gms.location.LocationRequest#setInterval()

The following examples show how to use com.google.android.gms.location.LocationRequest#setInterval() . 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: MainActivity.java    From location-samples with Apache License 2.0 6 votes vote down vote up
/**
 * Sets up the location request. Android has two location request settings:
 * {@code ACCESS_COARSE_LOCATION} and {@code ACCESS_FINE_LOCATION}. These settings control
 * the accuracy of the current location. This sample uses ACCESS_FINE_LOCATION, as defined in
 * the AndroidManifest.xml.
 * <p/>
 * When the ACCESS_FINE_LOCATION setting is specified, combined with a fast update
 * interval (5 seconds), the Fused Location Provider API returns location updates that are
 * accurate to within a few feet.
 * <p/>
 * These settings are appropriate for mapping applications that show real-time location
 * updates.
 */
private void createLocationRequest() {
    mLocationRequest = new LocationRequest();

    // Sets the desired interval for active location updates. This interval is
    // inexact. You may not receive updates at all if no location sources are available, or
    // you may receive them slower than requested. You may also receive updates faster than
    // requested if other applications are requesting location at a faster interval.
    mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);

    // Sets the fastest rate for active location updates. This interval is exact, and your
    // application will never receive updates faster than this value.
    mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);

    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
 
Example 2
Source File: HomeFragment.java    From SEAL-Demo with MIT License 6 votes vote down vote up
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(UPDATE_TIME);
    mLocationRequest.setFastestInterval(UPDATE_TIME);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (ActivityCompat.checkSelfPermission(myContext,
                Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            //Location Permission already granted
            mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
            mMap.setMyLocationEnabled(true);
        }
    } else {
        mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
        mMap.setMyLocationEnabled(true);
    }
}
 
Example 3
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 4
Source File: CoordinatesView.java    From dhis2-android-capture-app with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void startRequestingLocation() {
    LocationRequest locationRequest = new LocationRequest();
    locationRequest.setInterval(5000);
    locationRequest.setFastestInterval(1000);
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    locationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {
            if (locationResult != null) {
                Double latitude = locationResult.getLocations().get(0).getLatitude();
                Double longitude = locationResult.getLocations().get(0).getLongitude();
                updateLocation(GeometryHelper.createPointGeometry(longitude, latitude));
                mFusedLocationClient.removeLocationUpdates(locationCallback);
            }
        }
    };

    if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions((ActivityGlobalAbstract) getContext(),
                new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                ACCESS_LOCATION_PERMISSION_REQUEST);
    } else
        mFusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, null);

}
 
Example 5
Source File: LocationUpdatesService.java    From location-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the location request parameters.
 */
private void createLocationRequest() {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
    mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
 
Example 6
Source File: MainActivity.java    From OpenCircle with GNU General Public License v3.0 5 votes vote down vote up
protected LocationRequest makeLocationRequest()
{
    LocationRequest mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(0);
    mLocationRequest.setFastestInterval(0);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    return mLocationRequest;
}
 
Example 7
Source File: LocationProvider.java    From LocationAware with Apache License 2.0 5 votes vote down vote up
@NonNull private LocationRequest getLocationRequest() {
  LocationRequest locationRequest = new LocationRequest();
  locationRequest.setInterval(LOCATION_UPDATE_INTERVAL);
  locationRequest.setFastestInterval(LOCATION_UPDATE_FASTEST_INTERVAL);
  locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
  return locationRequest;
}
 
Example 8
Source File: MapsActivity.java    From Krishi-Seva with MIT License 5 votes vote down vote up
@Override
public void onConnected(Bundle bundle) {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(1000);
    mLocationRequest.setFastestInterval(1000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED) {
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    }
}
 
Example 9
Source File: OpenLocateHelper.java    From openlocate-android with MIT License 5 votes vote down vote up
private void buildLocationRequest() {
    long locationUpdateInterval = configuration.getLocationUpdateInterval() * 1000;
    long locationTransmissionInterval = configuration.getTransmissionInterval() * 1000;
    int locationAccuracy = configuration.getLocationAccuracy().getLocationRequestAccuracy();

    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(locationUpdateInterval);
    mLocationRequest.setFastestInterval(locationUpdateInterval / 2);
    mLocationRequest.setMaxWaitTime(Math.max(locationUpdateInterval * 2, (int)(locationTransmissionInterval * 0.85 / 3)));
    mLocationRequest.setPriority(locationAccuracy);
}
 
Example 10
Source File: FusedLocationModule.java    From react-native-fused-location with MIT License 5 votes vote down vote up
private LocationRequest buildLR() {
    LocationRequest request = new LocationRequest();
    request.setPriority(mLocationPriority);
    request.setInterval(mLocationInterval);
    request.setFastestInterval(mLocationFastestInterval);
    request.setSmallestDisplacement(mSmallestDisplacement);
    return request;
}
 
Example 11
Source File: MainActivity.java    From android-location-service with Apache License 2.0 5 votes vote down vote up
@Override
public void onLocationServicesConnectionSuccessful() {
    LocationRequest request = LocationRequest.create();
    request.setInterval(5000); // Five seconds

    mBackgroundLocationService.requestUpdates(request);
}
 
Example 12
Source File: LocationUpdatesBroadcastReceiver.java    From background_location_updates with Apache License 2.0 5 votes vote down vote up
@SuppressLint("MissingPermission")
public static void startTrackingBroadcastBased(Context context, int requestInterval) {
    FusedLocationProviderClient client = LocationServices.getFusedLocationProviderClient(context);
    LocationRequest request = new LocationRequest();
    request.setInterval(requestInterval);
    request.setFastestInterval(requestInterval);
    request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    client.requestLocationUpdates(request, LocationUpdatesBroadcastReceiver.getPendingIntent(context));
}
 
Example 13
Source File: MobicomLocationActivity.java    From Applozic-Android-SDK with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void onConnected(Bundle bundle) {
    try {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
        if (mCurrentLocation == null) {
            Toast.makeText(this, R.string.waiting_for_current_location, Toast.LENGTH_SHORT).show();
            locationRequest = new LocationRequest();
            locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
            locationRequest.setInterval(UPDATE_INTERVAL);
            locationRequest.setFastestInterval(FASTEST_INTERVAL);
            LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
        }

        if (mCurrentLocation != null) {
            mapFragment.getMapAsync(this);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

}
 
Example 14
Source File: MainActivity.java    From background-location-updates-android-o with Apache License 2.0 5 votes vote down vote up
/**
 * Sets up the location request. Android has two location request settings:
 * {@code ACCESS_COARSE_LOCATION} and {@code ACCESS_FINE_LOCATION}. These settings control
 * the accuracy of the current location. This sample uses ACCESS_FINE_LOCATION, as defined in
 * the AndroidManifest.xml.
 * <p/>
 * When the ACCESS_FINE_LOCATION setting is specified, combined with a fast update
 * interval (5 seconds), the Fused Location Provider API returns location updates that are
 * accurate to within a few feet.
 * <p/>
 * These settings are appropriate for mapping applications that show real-time location
 * updates.
 */
private void createLocationRequest() {
    mLocationRequest = new LocationRequest();

    mLocationRequest.setInterval(UPDATE_INTERVAL);

    // Sets the fastest rate for active location updates. This interval is exact, and your
    // application will never receive updates faster than this value.
    mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    // Sets the maximum time when batched location updates are delivered. Updates may be
    // delivered sooner than this interval.
    mLocationRequest.setMaxWaitTime(MAX_WAIT_TIME);
}
 
Example 15
Source File: AndroidLocationProvider.java    From BLE-Indoor-Positioning with Apache License 2.0 5 votes vote down vote up
private LocationRequest createHighAccuracyLocationRequest() {
    LocationRequest locationRequest = new LocationRequest();
    locationRequest.setInterval(TimeUnit.SECONDS.toMillis(3));
    locationRequest.setFastestInterval(TimeUnit.SECONDS.toMillis(1));
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    return locationRequest;
}
 
Example 16
Source File: LocationTracker.java    From SampleApp with Apache License 2.0 5 votes vote down vote up
/**
 * Creating location request object
 */
private void createLocationRequest() {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(UPDATE_INTERVAL);
    mLocationRequest.setFastestInterval(FATEST_INTERVAL);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setSmallestDisplacement(DISPLACEMENT);
}
 
Example 17
Source File: Location.java    From UberClone with MIT License 4 votes vote down vote up
private void inicializeLocationRequest(){
    locationRequest=new LocationRequest();
    locationRequest.setInterval(10000);
    locationRequest.setFastestInterval(3000);
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
 
Example 18
Source File: PlaceSearchFragment.java    From Place-Search-Service with MIT License 4 votes vote down vote up
private void createLocationRequest() {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
    mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
 
Example 19
Source File: MainActivity.java    From GNSS_Compare with Apache License 2.0 4 votes vote down vote up
/**
 * Registers GNSS measurement event manager callback.
 */
private void registerLocationManager() {

    mLocationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

    gnssCallback = new GnssMeasurementsEvent.Callback() {
        @Override
        public void onGnssMeasurementsReceived(GnssMeasurementsEvent eventArgs) {
            super.onGnssMeasurementsReceived(eventArgs);

            Log.d(TAG, "onGnssMeasurementsReceived (MainActivity): invoked!");

            if (rawMeasurementsLogger.isStarted())
                rawMeasurementsLogger.onGnssMeasurementsReceived(eventArgs);
        }
    };

    final LocationRequest locationRequest = new LocationRequest();

    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setMaxWaitTime(500);
    locationRequest.setInterval(1000);
    locationRequest.setFastestInterval(100);

    locationCallback = new LocationCallback(){
        @Override
        public void onLocationResult(LocationResult locationResult) {

            final Location lastLocation = locationResult.getLocations().get(locationResult.getLocations().size()-1);

            if(lastLocation != null) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        for(DataViewer dataViewer : mPagerAdapter.getViewers()) {
                            dataViewer.onLocationFromGoogleServicesResult(lastLocation);
                        }
                    }
                });

                Log.i(TAG, "locationFromGoogleServices: New location (phone): "
                        + lastLocation.getLatitude() + ", "
                        + lastLocation.getLongitude() + ", "
                        + lastLocation.getAltitude());

            }
        }
    };

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
            || ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

        mFusedLocationClient.requestLocationUpdates(
                locationRequest,
                locationCallback,
                null);

        mLocationManager.registerGnssMeasurementsCallback(
                gnssCallback);


    }
}
 
Example 20
Source File: LocationGetLocationActivity.java    From coursera-android with MIT License 4 votes vote down vote up
private void continueAcquiringLocations() {

        // Start location services
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(POLLING_FREQ);
        mLocationRequest.setFastestInterval(FASTEST_UPDATE_FREQ);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);


        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                .addLocationRequest(mLocationRequest);

        // Used if needed to turn on settings related to location services
        SettingsClient client = LocationServices.getSettingsClient(this);
        Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());
        task.addOnSuccessListener(this, new OnSuccessListener<LocationSettingsResponse>() {
            @Override
            public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
                // All location settings are satisfied. The client can initialize location requests here.
                if (checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                    mLocationCallback = getLocationCallback();
                    mLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, null);

                    // Schedule a runnable to stop location updates after a period of time
                    Executors.newScheduledThreadPool(1).schedule(new Runnable() {
                        @Override
                        public void run() {
                            mLocationClient.removeLocationUpdates(mLocationCallback);
                        }
                    }, MEASURE_TIME, TimeUnit.MILLISECONDS);
                }
            }
        });

        task.addOnFailureListener(this, new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                int statusCode = ((ApiException) e).getStatusCode();
                switch (statusCode) {
                    case CommonStatusCodes.RESOLUTION_REQUIRED:
                        // Location settings are not satisfied, but this can be fixed
                        // by showing the user a dialog.
                        try {
                            // Show the dialog by calling startResolutionForResult(),
                            // and check the result in onActivityResult().
                            ResolvableApiException resolvable = (ResolvableApiException) e;
                            resolvable.startResolutionForResult(LocationGetLocationActivity.this,
                                    REQUEST_CHECK_SETTINGS);
                        } catch (IntentSender.SendIntentException sendEx) {
                            // Ignore the error.
                        }
                        break;
                    case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                        // Location settings are not satisfied. However, we have no way
                        // to fix the settings so we won't show the dialog.
                        break;
                }
            }
        });
    }