【问题标题】:How to update current location latitude and longitude for Real Time Database如何更新实时数据库的当前位置纬度和经度
【发布时间】:2018-11-04 04:14:11
【问题描述】:

我正在使用 FusedLocationProviderClient` 获取当前设备位置

com.google.android.gms.tasks.Task<Location> location = mFusedLocationProviderClient.getLastLocation();
                location.addOnCompleteListener(new OnCompleteListener<Location>() {
                    @Override
                    public void onComplete(@NonNull com.google.android.gms.tasks.Task<Location> task) {
                        if(task.isSuccessful()){
                            Location currentLocation = task.getResult();

                            Toast.makeText(getApplicationContext(),"Detecting current Location",Toast.LENGTH_LONG).show();

                            moveCamera(new LatLng(currentLocation.getLatitude(),currentLocation.getLongitude()),ZOOM,"My Location");


                            mUserLocalStore = new UserLocalStore(getApplicationContext());
                            db = FirebaseDatabase.getInstance();
                            mRef = db.getReference("Location");
                            mRef.setValue(String.valueOf(mUserLocalStore.getDetails().getEmail()+" "+currentLocation.getLatitude()+" ,"+String.valueOf(currentLocation.getLongitude())));

并在firebase中保存位置,如何在更改位置或指定时间段时将位置更新到firebase`

【问题讨论】:

    标签: android firebase dictionary firebase-realtime-database location


    【解决方案1】:

    检查这个小例子,它应该可以解决您的问题。请检查 onConnected() 方法,有你需要的逻辑,如果你需要,我也让你所有的代码让它一起工作:

     package current_location_to_firebase.mytrendin.com.currentlocation;
    
    
        import android.location.Location;
        import android.os.Bundle;
    
        import android.support.v7.app.AppCompatActivity;
        import android.util.Log;
        import android.view.Menu;
        import android.view.MenuItem;
        import android.view.View;
        import android.widget.Button;
        import android.widget.TextView;
        import android.widget.Toast;
    
        import com.google.android.gms.common.ConnectionResult;
        import com.google.android.gms.common.api.GoogleApiClient;
        import com.google.android.gms.location.LocationServices;
        import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
        import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
        import com.google.firebase.FirebaseApp;
        import com.google.firebase.database.DatabaseReference;
        import com.google.firebase.database.FirebaseDatabase;
    
    
        public class MainActivity extends AppCompatActivity implements
                ConnectionCallbacks, OnConnectionFailedListener {
    
            private static final String TAG = "CurrentLocationApp";
            private GoogleApiClient mGoogleApiClient;
            private Location mLastLocation;
            private TextView mLatitudeText;
            private TextView mLongitudeText;
            private FirebaseDatabase mFirebaseDatabase;
            private DatabaseReference mLocationDatabaseReference;
            Button saveLocationToFirebase;
            String value_lat = null;
            String value_lng=null;
    
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
    
                FirebaseApp.initializeApp(this);
                mFirebaseDatabase = FirebaseDatabase.getInstance();
                mLocationDatabaseReference= mFirebaseDatabase.getReference().child("my current location");
                mLatitudeText = (TextView) findViewById((R.id.latitude_text));
                mLongitudeText = (TextView) findViewById((R.id.longitude_text));
                saveLocationToFirebase=(Button)findViewById(R.id.save_location);
                buildGoogleApiClient();
            }
    
            protected synchronized void buildGoogleApiClient() {
                mGoogleApiClient = new GoogleApiClient.Builder(this)
                        .addConnectionCallbacks(this)
                        .addOnConnectionFailedListener(this)
                        .addApi(LocationServices.API)
                        .build();
            }
    
            @Override
            protected void onStart() {
                super.onStart();
                mGoogleApiClient.connect();
            }
    
            @Override
            protected void onStop() {
                super.onStop();
                if (mGoogleApiClient.isConnected()) {
                    mGoogleApiClient.disconnect();
                }
            }
            @Override
            public void onConnected(Bundle connectionHint) {
                mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    
                    if (mLastLocation != null) {
    
                                value_lat= String.valueOf(mLastLocation.getLatitude());
                                value_lng =String.valueOf(mLastLocation.getLongitude());
                        mLatitudeText.setText(value_lat);
                        mLongitudeText.setText(value_lng);
    
                        saveLocationToFirebase.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                mLocationDatabaseReference.push().setValue("Latitude : "+value_lat +"  &amp; Longitude : "+value_lng);
                                Toast.makeText(MainActivity.this ,"Location saved to the Firebasedatabase",Toast.LENGTH_LONG).show();
                            }
                        });
                    }
            }
            @Override
            public void onConnectionFailed(ConnectionResult result) {
                Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + result.getErrorCode());
            }
             @Override
            public void onConnectionSuspended(int cause) {
                Log.i(TAG, "Connection suspended");
                mGoogleApiClient.connect();
            }
    
        }
    

    请记住在您的数据库规则中将其设置为 true,如果它们被设置为 false,则只有 auth 用户才能发布到其中

    saveLocationToFirebase 是一个应在您的 XML 中初始化的按钮,因此当我们单击它时,您会将值更新到数据库中,如果您希望它们自己执行此操作,请将侦听器附加到纬度和经度,所以,当他们改变时会提示上传到数据库。

    【讨论】:

    • 这是否可以将相同 id 的位置连续更新到 fire-base 数据库?
    • 是的,但在这个例子中你需要按下一个按钮,自己设置一个 thread.sleep 并且你可以在每次放入它时发送数据,所以你可以做例如, Thread.sleep(5000) ... 它会每 5 秒发送一次数据,但为了做到这一点,您每次想要更新数据库时都需要访问 Thread.sleep
    • 如果你能给我一个访问thread.sleep的示例代码方式,那就太好了
    • 你能给我一个示例代码吗?我尝试了更多时间,但都没有工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-11
    • 1970-01-01
    • 2011-08-31
    • 1970-01-01
    • 2017-05-09
    • 1970-01-01
    相关资源
    最近更新 更多