如何在我们走路时获得实时的当前位置更新
Posted
技术标签:
【中文标题】如何在我们走路时获得实时的当前位置更新【英文标题】:How to get real time current location update as we walk 【发布时间】:2017-12-13 04:59:37 【问题描述】:我想讨论是否有可能在我们移动时获得实时当前位置更新,我尝试使用 FusedLocationProviderClient 请求每 2 分钟更新一次当前位置
我们可以做些什么来实现这一点,为 walk 实现 detectActivity 并在其中每 2 分钟或更短时间请求一次当前位置更新?
或者在 FusedLocationProviderClient 中有位置更改监听器?以便我们可以监听位置是否发生变化并实时获取 latlng 并在应用程序的 textview 中显示?位置更改侦听器在 GoogleMap 对象上可用,但不建议现在使用,对吗?
还是我应该坚持 FusedLocationProviderClient 的请求更新?
先谢谢了
另外告诉我,如果我把手机从一个房间搬到另一个房间,latlng 会有变化吗?
【问题讨论】:
您可以使用 GoogleMap.OnMyLocationChangeListener,因为它已被折旧,但可以按您的意愿工作 即使我们移动 10 步也会调用 locationchangelistener 吗? 可能是的。试试看 【参考方案1】:你可以使用我创建的这个类:
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'
希望这会有所帮助。
【讨论】:
以上是关于如何在我们走路时获得实时的当前位置更新的主要内容,如果未能解决你的问题,请参考以下文章