2016-10-10 11 views
0

現在の位置の経度と緯度を取得し、Firebaseデータベースに保持しようとしています。ソースコードは正常に動作しますが、経度と緯度は0.0として保存されます。場所(経度、緯度)は0を返します

this is how it looks like in the database.

どのように私はこれを修正し、正しい現在位置を取得するのですか?数十の輸入授業は気にしないで、私はまだ何も取り除かなかった。

重複する質問があれば、本当に申し訳ありません。

public class Register extends AppCompatActivity implements View.OnClickListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { 
    EditText edtemail, edtpass, edtnumber; 
    Button btnRegister, btnLogin, forgotpwdbtn; 
    private FirebaseAuth auth; 
    private DatabaseReference mDatabase; 


    private GoogleApiClient googleApiClient; 
    private Location location; 
    private double longitude; 
    private double latitude; 
    String mUserId; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_register); 


     btnRegister = (Button) findViewById(R.id.btnRegister); 
     btnLogin = (Button) findViewById(R.id.btnLogin); 
     forgotpwdbtn = (Button) findViewById(R.id.forgotpwdbtn); 

     btnRegister.setOnClickListener(this); 
     btnLogin.setOnClickListener(this); 
     forgotpwdbtn.setOnClickListener(this); 
     auth = FirebaseAuth.getInstance(); 


     mDatabase = FirebaseDatabase.getInstance().getReference(); 
     mUserId = FirebaseAuth.getInstance().getCurrentUser().getUid(); 

     edtemail = (EditText) findViewById(R.id.edtemail); 
     edtpass = (EditText) findViewById(R.id.edtpass); 
     edtnumber = (EditText) findViewById(R.id.edtnumber); 


    } 


    public void onClick(View view) { 

     switch (view.getId()) { 

      case R.id.forgotpwdbtn: 
       Intent passwordIntent = new Intent(Register.this, ForgotPasswordActivity.class); 
       startActivity(passwordIntent); 
       break; 
      case R.id.btnLogin: 
       Intent intent = new Intent(Register.this, LoginActivity.class); 
       startActivity(intent); 
       break; 
      case R.id.btnRegister: 
       googleApiClient = new GoogleApiClient.Builder(this) 
         .addConnectionCallbacks(this) 
         .addOnConnectionFailedListener(this) 
         .addApi(LocationServices.API) 
         .build(); 

       final String key = mDatabase.child("data").push().getKey(); 
       final String email = edtemail.getText().toString().trim(); 
       final String password = edtpass.getText().toString().trim(); 
       final String number = edtnumber.getText().toString().trim(); 
       if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
        // TODO: Consider calling 
        // ActivityCompat#requestPermissions 
        // here to request the missing permissions, and then overriding 
        // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
        //           int[] grantResults) 
        // to handle the case where the user grants the permission. See the documentation 
        // for ActivityCompat#requestPermissions for more details. 
        return; 
       } 
       location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient); 
       if (location != null) { 
        //Getting longitude and latitude 
        longitude = location.getLongitude(); 
        latitude = location.getLatitude(); 
       } 

       String lon = String.valueOf(longitude); 
       String lat = String.valueOf(latitude); 

       Item newItem = new Item(email, password, number, lon, lat); 
       Map<String, Object> itemValues = newItem.toMap(); 
       Map<String, Object> childUpdates = new HashMap<>(); 
       childUpdates.put("/data/" + mUserId + "/" + key, itemValues); 
       mDatabase.updateChildren(childUpdates); 

        auth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(Register.this, new OnCompleteListener<AuthResult>() { 


         @Override 
         public void onComplete(@NonNull Task<AuthResult> task) { 

          if (task.isSuccessful()) { 

           Toast.makeText(Register.this, "Registration success", Toast.LENGTH_SHORT).show(); 
           Intent intent = new Intent(Register.this, LoginActivity.class); 
           startActivity(intent); 

          } else { 
           Toast.makeText(Register.this, "Registration failed." + task.getException(), Toast.LENGTH_SHORT).show(); 
          } 
         } 
        }); 
        break; 
       } 
     } 

    @Override 
    public void onConnected(@Nullable Bundle bundle) { 

    } 

    @Override 
    public void onConnectionSuspended(int i) { 

    } 

    @Override 
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 

    } 
} 

答えて

1

あなたはGoogleApiClientに接続していません。 onCreateメソッドでGoogleApiClientオブジェクトを作成します。さらに2つのメソッド、onStartonStopをオーバーライドします。 onStartメソッドでは、googleapiclient.connect()を呼び出してGoogleApiClientに接続します。 onStopメソッドでは、if(googleapiclient.isConnected()) { googleapiclient.disconnect() }を呼び出してクライアントを切断します。

+0

あなたが言ったとおりにしましたが、登録ボタンを押すと新しいエラーが発生しました。 "ヌルオブジェクト参照で仮想メソッド 'void com.google.android.gms.common.api.GoogleApiClient.connect()'を呼び出そうとしました。" –

+0

あなたは 'googleApiClient = new GoogleApiClient.Builder (this)).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build(); 'onCreate'メソッドへのコード?? –

+0

はい、私は同じ、まだ同じでした。登録ボタンを押した後に停止します。 –

関連する問題