我们走路时如何更新实时当前位置

时间:2017-07-09 10:35:22

标签: java android google-maps google-maps-android-api-2 android-location

我想讨论是否有可能在移动时获得实时当前位置更新,我尝试使用FusedLocationProviderClient每2分钟请求当前位置更新

我们可以做些什么来实现这一点,为walk执行detectActivity并每2分钟或更短时间请求当前位置更新?

还是在FusedLocationProviderClient中有位置更改侦听器的东西?这样我们就可以监听位置是否有变化并实时获取并在应用程序的textview中显示?位置更改侦听器在GoogleMap对象上可用,但现在不建议使用,我是对的吗?

还是应该坚持使用FusedLocationProviderClient的requestupdate?

事先谢谢

还告诉我,如果我将手机从一个房间移动到另一个房间,是否会有更改?

1 个答案:

答案 0 :(得分:0)

您可以使用我创建的这个类:

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();
}


}

在您的活动的onStart中,请致电:

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

在onStop中使用此:

locationFetcher.disconnectGps();

您将在该类的onLocationChangedMethod中获取位置更新。

您还必须添加此依赖项:

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

希望这有帮助。