在location_switch
点击用户上线并检查当前位置。但是在点击开关时,它没有显示我当前的位置。我正在使用模拟器来检查应用程序。即使我在模拟器中更改位置,但它无法正常工作。检查位置后,它将存储在 Firebase数据库中,但正在运行,但我的当前位置未显示。
location_switch.setOnCheckedChangeListener(new MaterialAnimatedSwitch.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(boolean isOnline) {
if (isOnline) {
startLocationUpdates();
displayLocation();
Snackbar.make(mapFragment.getView(), "You are online", Snackbar.LENGTH_SHORT)
.show();
} else {
stopLocationUpdates();
mCurrent.remove();
Snackbar.make(mapFragment.getView(), "You are offline", Snackbar.LENGTH_SHORT)
.show();
}
}
});
}
private void stopLocationUpdates() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
private void displayLocation() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLastLocation != null) {
if (location_switch.isChecked()) {
final double latitute = mLastLocation.getLatitude();
final double longitude = mLastLocation.getLongitude();
/*Update To FireBase*/
geoFire.setLocation(FirebaseAuth.getInstance().getCurrentUser().getUid(), new GeoLocation(latitute, longitude), new GeoFire.CompletionListener() {
@Override
public void onComplete(String key, DatabaseError error) {
/*Add Marker*/
if (mCurrent != null) {
mCurrent.remove();
mCurrent = mMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.car))
.position(new LatLng(latitute, longitude))
.title("You"));
/*Move camera to this postion*/
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitute, latitute), 15.0f));
}
}
});
}
} else {
Log.d("ERROR", "Can Not Get Your Location: ");
}
}
private void startLocationUpdates() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
}
@Override
public void onLocationChanged(Location location) {
mLastLocation = location;
displayLocation();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
displayLocation();
startLocationUpdates();
}
@Override
public void onConnectionSuspended(int i) {
mGoogleApiClient.connect();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
}
任何人都可以帮我弄清楚我做错了吗?
答案 0 :(得分:0)
您已在许多地方调用了displayLocation。因此,在goOnline上只需调用startLocationUpdate并在onLocationChanged中调用displayLocation。由于locationUpdates等待android系统根据你的FASTEST INTERVAL触发下一个事件,你将无法获得最新的位置。同时将FASTEST INTERVAL设置为1秒以进行即时位置更新。希望这会有所帮助。