我正在创建一个应用程序,该应用程序使用其UID定期存储和更新每个用户的纬度和经度。当前,当用户首次注册时,他们的经/纬度已成功捕获,并使用Geofire上载到了我的Firebase数据库中,并存储在名为“ Geo”的表中。但是,我也尝试在应用程序的主要活动上实现类似的代码,以使lat / long用户不断更新。
我当前的主要活动代码未标记任何错误,但是初始注册后经/纬度用户未更新。有谁知道为什么会这样吗?
主要活动(切掉尽可能多的不相关代码):
public class FindChatters extends Fragment implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener {
GoogleApiClient mGoogleAPIClient;
Location mLastLocation;
LocationRequest mLocationRequest;
public FindChatters() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mMainView = inflater.inflate(R.layout.fragment_find_chatters, container, false);
mUsersDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
...
return mMainView;
}
//RETRIEVE DATA IN REALTIME
@Override
public void onStart() {
super.onStart();
startListening();
}
public void startListening() {
...
//
//BUILD API CLIENT TO GET LOCATION UPDATES
protected synchronized void buildGoogleAPIClient() {
mGoogleAPIClient = new GoogleApiClient.Builder(getActivity()).addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).addApi(LocationServices.API)
.build();
mGoogleAPIClient.connect();
}
//UPDATE USERS LOCATION OVER A SPECIFIED PERIOD
@Override
public void onConnected(@Nullable Bundle bundle) {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(1000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if (ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && A ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleAPIClient, mLocationRequest, this);
buildGoogleAPIClient();
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
//SAVE USERS UPDATED LOCATION TO THE DATABASE
@Override
public void onLocationChanged(Location location) {
mLastLocation = location;
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
String userID = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Geo");
//ADD THE UPDATED USER LAT/LNG TO THE DATABASE
GeoFire geoFire = new GeoFire(ref);
geoFire.setLocation(userID, new GeoLocation(location.getLatitude(), location.getLongitude()));
}
}
与往常一样,非常感谢您的帮助。
Android Studio v3.1.3
答案 0 :(得分:0)
首先不建议使用LocationServices.FusedLocationApi.requestLocationUpdates
,请尝试使用mFusedLocationClient.requestLocationUpdates(getLocationRequest(), mLocationCallback, null);
(从com.google.android.gms.location.LocationCallback
进行回调)。
第二个您想在应用运行时更新位置吗?如果您需要一直进行更新,请查看FirebaseJobDispatcher,它将为您提供每隔n分钟(至少每20分钟)开始更新并保存一次的选项,
也来自文档: 此间隔不精确。您可能根本不会收到更新(如果没有可用的位置来源),或者收到的更新速度可能比要求的要慢。您可能还会比要求的接收速度更快(如果其他应用程序以更快的间隔请求定位)。可以通过setFastestInterval(long)控制您将收到的最快更新速率。默认情况下,此最快速率是间隔频率的6倍。 https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest
最后,Geofire setLocation具有回调,您可以在此处进行日志记录
GeoFire geoFire = new GeoFire(ref);
geoFire.setLocation(userID, new GeoLocation(lat, lng), new GeoFire.CompletionListener() {
@Override
public void onComplete(String key, DatabaseError error) {
Log.d(TAG, "onComplete: " );
}
});
PS:如果您的locationResult!= null,则将在LocationCallback中检查良好做法,如果位置服务未在设备上运行,则它将为null。