How to get real time current location update as we walk

雨燕双飞 提交于 2019-12-13 03:48:24

问题


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:


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.



来源:https://stackoverflow.com/questions/44995445/how-to-get-real-time-current-location-update-as-we-walk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!