Search in sources :

Example 1 with FusedLocationProviderClient

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) {
    }
}
Also used : LocationRequest(com.google.android.gms.location.LocationRequest) LocationCallback(com.google.android.gms.location.LocationCallback) FusedLocationProviderClient(com.google.android.gms.location.FusedLocationProviderClient) LocationResult(com.google.android.gms.location.LocationResult) Location(android.location.Location)

Example 2 with FusedLocationProviderClient

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();
                    }
                });
            }
        }
    });
}
Also used : GoogleMap(com.google.android.gms.maps.GoogleMap) UiSettings(com.google.android.gms.maps.UiSettings) OnMapReadyCallback(com.google.android.gms.maps.OnMapReadyCallback) FusedLocationProviderClient(com.google.android.gms.location.FusedLocationProviderClient) LatLng(com.google.android.gms.maps.model.LatLng) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) ApiException(com.google.android.gms.common.api.ApiException) ResolvableApiException(com.google.android.gms.common.api.ResolvableApiException) Location(android.location.Location)

Example 3 with FusedLocationProviderClient

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) {
    }
}
Also used : Context(android.content.Context) PowerManager(android.os.PowerManager) Handler(android.os.Handler) FusedLocationProviderClient(com.google.android.gms.location.FusedLocationProviderClient) Location(android.location.Location) SuppressLint(android.annotation.SuppressLint)

Example 4 with FusedLocationProviderClient

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();
        }
    }
}
Also used : FusedLocationProviderClient(com.google.android.gms.location.FusedLocationProviderClient) LatLng(com.google.android.gms.maps.model.LatLng) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) ApiException(com.google.android.gms.common.api.ApiException) ResolvableApiException(com.google.android.gms.common.api.ResolvableApiException) Location(android.location.Location)

Example 5 with FusedLocationProviderClient

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();
        }
    }
}
Also used : FusedLocationProviderClient(com.google.android.gms.location.FusedLocationProviderClient) LatLng(com.google.android.gms.maps.model.LatLng) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) ApiException(com.google.android.gms.common.api.ApiException) ResolvableApiException(com.google.android.gms.common.api.ResolvableApiException) Location(android.location.Location)

Aggregations

Location (android.location.Location)5 FusedLocationProviderClient (com.google.android.gms.location.FusedLocationProviderClient)5 ApiException (com.google.android.gms.common.api.ApiException)3 ResolvableApiException (com.google.android.gms.common.api.ResolvableApiException)3 LatLng (com.google.android.gms.maps.model.LatLng)3 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)3 SuppressLint (android.annotation.SuppressLint)1 Context (android.content.Context)1 Handler (android.os.Handler)1 PowerManager (android.os.PowerManager)1 LocationCallback (com.google.android.gms.location.LocationCallback)1 LocationRequest (com.google.android.gms.location.LocationRequest)1 LocationResult (com.google.android.gms.location.LocationResult)1 GoogleMap (com.google.android.gms.maps.GoogleMap)1 OnMapReadyCallback (com.google.android.gms.maps.OnMapReadyCallback)1 UiSettings (com.google.android.gms.maps.UiSettings)1 OnSuccessListener (com.google.android.gms.tasks.OnSuccessListener)1