因此,我在Android Studio中为我的应用进行了Google Maps活动,我需要获取实时位置更新。目的是使地图上的标记只有在距离标记足够近的地方才能单击。 这是我目前的代码:
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setInterval(5000);
locationRequest.setFastestInterval(2500);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
if (locationResult == null) {
return;
}
mLastKnownLocation = locationResult.getLastLocation();
LatLng lastKnownLatLng = new LatLng(mLastKnownLocation.getLatitude(), mLastKnownLocation.getLongitude());
//THIS IS AN EXAMPLE OF THE DISTANCE VALUES
distanceHala = SphericalUtil.computeDistanceBetween(lastKnownLatLng, HalA);
mFusedLocationProviderClient.removeLocationUpdates(locationCallback);
}
};
mFusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, null);
稍后,我有一些if语句使它们可单击。现在的问题是,仅当我打开Map活动时,位置才会更新,而在活动运行时,位置不会更新。因此,当我在打开活动范围然后走到足够近的距离时超出范围时,即使我距离足够近,按钮也仍然无法单击。