How to get real time current location update as we

2019-08-18 20:38发布

I want to discuss if there is possibility of getting real time current location update as we move, I tried with FusedLocationProviderClient to request for current location update every 2 minutes

What can we do to achieve this, implement detectActivity for walk and request current location update every 2 minutes or less in it?

or there is on location change listener thing in FusedLocationProviderClient? so that we can listen for if there is change in location and get latlng in real time and display in textview in application? the location change listener is available on GoogleMap object but isn't recommended to use now, am I right?

or should i stick to requestupdate of FusedLocationProviderClient?

Thanks beforehand

Also tell me if i move my phone from one room to another, will there be change in latlng?

1条回答
Ridiculous、
2楼-- · 2019-08-18 21:06

You can use this class that i created :

public class LocationFetcher implements com.google.android.gms.location.LocationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
public GoogleApiClient googleApiClient;
Context context;
private LocationRequest locationRequest;

public LocationFetcher(Context context) {
    this.context = context;
    googleApiClient = new GoogleApiClient.Builder(context)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    locationRequest = new LocationRequest();


}

public void connectGps() {
    googleApiClient.connect();
}

public void requestLocationUpdate() {
    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 100);
    } else {
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
     //Getting location every 3 seconds. You can change this.
            locationRequest.setInterval(3000);
            locationRequest.setFastestInterval(3000);


        LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
    }
}

public boolean isConnected() {
    return googleApiClient.isConnected();
}

@Override
public void onLocationChanged(Location location) {
//You will get location updates here.
}


@Override
public void onConnected(@Nullable Bundle bundle) {

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

    builder.setAlwaysShow(true);
    PendingResult result = LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());

    if (result != null) {
        result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
            @Override
            public void onResult(LocationSettingsResult locationSettingsResult) {
                final Status status = locationSettingsResult.getStatus();
                switch (status.getStatusCode()) {
                    case LocationSettingsStatusCodes.SUCCESS:
                        requestLocationUpdate();
                        break;
                    case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                        try {
                            if (status.hasResolution()) {
                                status.startResolutionForResult((Activity) context, 1000);
                            }
                        } catch (IntentSender.SendIntentException e) {
                        }
                        break;
                    case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                        break;
                }
            }
        });
    }

}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    DialogPopup.getInstance().showLocationSettingsAlert(context);
}

public void removeLocationUpdates() {
   if (googleApiClient.isConnected()){
       LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this);
   }
}

public void disconnectGps() {
    googleApiClient.disconnect();
}


}

In onStart of your activity call this :

LocationFetcher locationFetcher = new LocationFetcher(this);
locationFetcher.connectGps();

And in onStop use this:

locationFetcher.disconnectGps();

You will get the location updates in the onLocationChangedMethod of the class.

You have to add this dependency too:

compile 'com.google.android.gms:play-services-location:10.2.1'

Hope this helps.

查看更多
登录 后发表回答