如何通过将纬度经度从地图获取到Firebase实时数据库中并在位置更改时进行更新来保存经度

时间:2018-11-01 15:34:36

标签: firebase

我正在研究模块,在该模块中,应用程序可以获取用户当前位置并通过Toast在onlongclick上显示纬度经度。我想在Firebase中保存用户的经度,并在位置更改时继续更新。怎么做?任何信息,详细的代码将不胜感激。 `

import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.GetTokenResult;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;

import java.util.HashMap;
import java.util.Map;

import de.hdodenhof.circleimageview.CircleImageView;

public class RegisterActivity extends AppCompatActivity {
   private EditText Name, Residence, Current;
   private Button register;
   private CircleImageView imageView;
    public static final int PICK_IMAGE = 1234;
    private Uri imageUri;
    private StorageReference mStorage;
    private FirebaseAuth mAuth;
    private FirebaseFirestore mfirestore;
    private ProgressBar progressBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        mStorage=FirebaseStorage.getInstance().getReference().child("images");
        mAuth=FirebaseAuth.getInstance();
        mfirestore=FirebaseFirestore.getInstance();

        Name=(EditText) this.findViewById(R.id.nameEdittext);
        Residence=(EditText) this.findViewById(R.id.residenceEditText);
        Current=(EditText) this.findViewById(R.id.CrrnteditText);
        register=(Button)findViewById(R.id.registerButton);
        imageView=(CircleImageView) findViewById(R.id.register_image_btn);
        progressBar=(ProgressBar)findViewById(R.id.progressBar);

        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (imageUri != null){
                    progressBar.setVisibility(View.VISIBLE);
                    final String name= Name.getText().toString();
                   String resid= Residence.getText().toString();
                   final String curent= Current.getText().toString();

                    if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(resid) && !TextUtils.isEmpty(curent)){
                        mAuth.createUserWithEmailAndPassword(name,curent).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                if (task.isSuccessful()){
                                    final String user_id= mAuth.getCurrentUser().getUid();
                                    StorageReference user_profile= mStorage.child(user_id+".jpg");
                                    user_profile.putFile(imageUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                                        @Override
                                        public void onComplete(@NonNull final Task<UploadTask.TaskSnapshot> uploadtask) {
                                            if (uploadtask.isSuccessful()) {
                                                final String download_url = uploadtask.getResult().getStorage().getDownloadUrl().toString();
                                                FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(new OnSuccessListener<InstanceIdResult>() {
                                                    @Override
                                                    public void onSuccess(InstanceIdResult instanceIdResult) {
                                                     final String token_id= instanceIdResult.getToken();
                                                        Log.e("token_id", token_id);
                                                        Map<String, Object> userMap = new HashMap<>();
                                                        userMap.put("name", name);
                                                        userMap.put("image", download_url);
                                                        userMap.put("Current Location", curent);
                                                        userMap.put("token_id", token_id);
                                                        mfirestore.collection("Users").document(user_id).set(userMap).addOnSuccessListener(new OnSuccessListener<Void>() {
                                                            @Override
                                                            public void onSuccess(Void aVoid) {
                                                                progressBar.setVisibility(View.INVISIBLE);
                                                                sendToNavigation();
                                                            }
                                                        });

                                                    }
                                                });

                                                    }
                                                    else {
                                                    Toast.makeText(RegisterActivity.this,"Error :" + uploadtask.getException().getMessage(), Toast.LENGTH_SHORT).show();
                                                    progressBar.setVisibility(View.INVISIBLE);

                                                }

                                            }
                                                });
                                    }else
                            {
                                Toast.makeText(RegisterActivity.this, "Error :" + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                                progressBar.setVisibility(View.INVISIBLE);
                            }

                            }
                        });

                    }
                }
            }
        });
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);

            }
        });

    }

    private void sendToNavigation() {
        Intent intent= new Intent(RegisterActivity.this, NavigationActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PICK_IMAGE){
            imageUri=data.getData();
            imageView.setImageURI(imageUri);
        }
    }

    public void onClick(View view) {
    }
}

`

1 个答案:

答案 0 :(得分:0)

GeoFire是您的首选。您可以直接使用Firebase集成来处理各种基于位置的数据:

链接:https://github.com/firebase/geofire-java