这就是我的Firebase中的实时数据库在发送用户当前位置时的样子,而不使用唯一的ID。
但是,当在代码中使用push()时,它会不断地在每次刷新位置时生成新的ID。
是否可以在不生成新的唯一id的情况下不断更新用户的位置或完全删除对位置的更新?
private GoogleMap mMap;
private GoogleApiClient mGoogleApiClient;
private Location mLocation;
private LocationManager mLocationManager;
private LocationRequest mLocationRequest;
private com.google.android.gms.location.LocationListener listener;
private long UPDATE_INTERVAL = 2000;
private long FASTEST_INTERVAL = 2000;
private LocationManager locationManager;
private LatLng latLng;
private boolean isPermission;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
if(requestSinglePermission()){
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
checkLocation();
}
}
private boolean checkLocation() {
if(!isLocationEnabled()){
showAlert();
}
return isLocationEnabled();
}
private void showAlert() {
final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Enable Location")
.setMessage("Your Locations Settings is set to 'Off'.\nPlease Enable Location to " +
"use this app")
.setPositiveButton("Location Settings", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(myIntent);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
}
});
dialog.show();
}
private boolean isLocationEnabled() {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}
private boolean requestSinglePermission() {
Dexter.withActivity(this)
.withPermission(Manifest.permission.ACCESS_FINE_LOCATION)
.withListener(new PermissionListener() {
@Override
public void onPermissionGranted(PermissionGrantedResponse response) {
isPermission = true;
}
@Override
public void onPermissionDenied(PermissionDeniedResponse response) {
// check for permanent denial of permission
if (response.isPermanentlyDenied()) {
isPermission = false;
}
}
@Override
public void onPermissionRationaleShouldBeShown(com.karumi.dexter.listener.PermissionRequest permission, PermissionToken token) {
}
}).check();
return isPermission;
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if(latLng!=null){
mMap.addMarker(new MarkerOptions().position(latLng).title("Marker in Current Location"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,14F));
}
}
@Override
public void onConnected(@Nullable Bundle bundle) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
return;
}
startLocationUpdates();
mLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLocation == null) {
startLocationUpdates();
}
else {
Toast.makeText(this, "Location not Detected", Toast.LENGTH_SHORT).show();
}
}
private void startLocationUpdates() {
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(UPDATE_INTERVAL);
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
mLocationRequest, this);
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
@Override
public void onLocationChanged(Location location) {
String msg = "Updated Location: " +
Double.toString(location.getLatitude()) + "," +
Double.toString(location.getLongitude());
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
LocationSend send = new LocationSend(
location.getLongitude(),
location.getLatitude()
);
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users");
reference.getKey();
reference.child("User Location").setValue(send).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()){
Toast.makeText(MapsActivity.this, "Location Saved", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(MapsActivity.this, "Location Not Saved", Toast.LENGTH_SHORT).show();
}
}
});
latLng = new LatLng(location.getLatitude(), location.getLongitude());
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
protected void onStart() {
super.onStart();
if(mGoogleApiClient !=null){
mGoogleApiClient.connect();
}
}
@Override
protected void onStop() {
super.onStop();
if(mGoogleApiClient.isConnected()){
mGoogleApiClient.disconnect();
}
}
最合适的解决方案是实现Firebase身份验证,并使用存储经纬度,如下所示:
Firebase-root
|
--- users
|
--- $uid
|
--- location
|
--- latitude: 37.421795
|
--- longitude: -122.13792
这样,您将始终更新相同的位置。以下是所需的参考资料:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference locationRef = db.child("users").child(uid).child("location");
我需要帮助找出使用Firebase的最佳方式。我正在制作一个twitter克隆,但与FireFeed不同,我的帖子是可变的。 我的数据库结构如下: 房间,基本上是一个你可以订阅的帖子提要。会议室成员可以向该会议室发帖: 房间/{房间ID}/帖子/{postId}/- 用户,所有用户的全局列表: 用户/{userId}/feed/{postId}- 帖子,实际帖子内容的全球列表: 贴子/{postI
下面是Firebase示例代码,我可以在其中更新经过身份验证的用户。(var user=firebase.auth()。当前用户;) 我想知道是否可以在不必登录的情况下编辑用户,将updateProfile功能与用户的UID或电子邮件一起使用。 谢谢
问题内容: 我用一个简单的方法来生成国家列表。每个列表中都有一个可以扩展和折叠的隐藏行/ div。 我面临的问题是,在将Angular引入应用程序之前,我手动对元素的ID进行了硬编码,例如: 新代码使用: 如何在我的帐户中分配动态/增量ID ? 问题答案: 您可以使用 https://docs.angularjs.org/api/ng/directive/ngRepeat
我想知道什么是电池效率最高的方式发送准确的位置更新到服务器/防火墙每5秒,即使应用程序关闭或手机重新启动。我尝试使用alarmmanager.setrepeating和android.intent.action.boot_completed接收器- 使用post一个延迟消息或runnable到一个处理程序不是一个可靠的解决方案,因为一旦应用程序被删除,位置更新就会停止。 是否有可靠的方法每5秒向服
我是Firebase的新手。最近,我试图将用户保存到Firebase中,但我做不到。当我生成APK并将其运行到真实设备(而不是模拟器)上时,新用户不会将其添加到Firebase中。此外,没有显示任何错误。任务正确执行并转到OTPActivity。这是我的完整代码: 公共无效保存用户(){ 请帮帮我。提前谢谢。
问题内容: 我想使用JavaScript强制文本框的值小写。我已经尝试过下面的代码,但是每次您按一个键,光标就会跳到输入的末尾。如何避免这种情况? 问题答案: $(“#beLowerCase”).on(‘input’, function(){ 这实际上也适用于CSS: 服务器可以照顾实际的下壳体…