1

テキストボックスのテキストを設定するために、getMyLocationAddressのonLocationChangedメソッドにある現在の場所にアクセスしたいと思います.Goocoder.getLocationFrom()で使用できるように、どの変数が正確に格納されているかを知りたいです。getMyLocationAddressメソッドのonLocationChangedで私の現在の場所にアクセスするにはどうすればいいですか?

geocoder.getFromLocation(latLng.getLatitude()、latLng.getLongitude()、1);でLatLngオブジェクトlatLngを使用しています。 これは私が推測するところでは現在の場所が格納されています。しかしgetFromLocationにはLocationオブジェクトが必要です。どのLocationオブジェクトをLatLngオブジェクトとして使用する必要がありますかはわかりませんが、エラーはありますか?

@Override 
    public void onLocationChanged(Location location) { 

     mLastLocation = location; 
     if (mCurrLocationMarker != null) { 
      mCurrLocationMarker.remove(); 
     } 

     //Place current location marker 
     LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); 
     MarkerOptions markerOptions = new MarkerOptions(); 
     markerOptions.position(latLng); 
     markerOptions.title("Current Position"); 
     markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)); 
     mCurrLocationMarker = mMap.addMarker(markerOptions); 

     //move map camera 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
     mMap.animateCamera(CameraUpdateFactory.zoomTo(50)); 

     //stop location updates 
     if (mGoogleApiClient != null) { 
      LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
     } 

    } 


public void getMyLocationAddress() { 

    Geocoder geocoder= new Geocoder(this, Locale.ENGLISH); 

    try { 

     //Place your latitude and longitude 
     //List<Address> addresses = geocoder.getFromLocation(37.423247,-122.085469, 1); 
     List<Address> addresses = geocoder.getFromLocation(latLng.getLatitude(),latLng.getLongitude(),1); 

     if(addresses != null) { 

      Address fetchedAddress = addresses.get(0); 
      StringBuilder strAddress = new StringBuilder(); 

      for(int i=0; i<fetchedAddress.getMaxAddressLineIndex(); i++) { 
       strAddress.append(fetchedAddress.getAddressLine(i)).append("\n"); 
      } 

      etOrigin.setText(strAddress.toString()); 

     } 

     else 
      etOrigin.setText("No location found..!"); 

    } 
    catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     Toast.makeText(getApplicationContext(),"Could not get address..!", Toast.LENGTH_LONG).show(); 
    } 
} 

答えて

1

変数の場所の種類を使用する必要があります。 https://developer.android.com/reference/android/location/Location.html

+0

はいしかし、その場所の変数は、私の現在の場所は、あなたの緯度経度変数はローカルvariable.itのみonLocationChanged()メソッドにアクセスすることが可能である –

+0

保存されています。

あなたはここで多くを読むことができます。グローバルに宣言して、getMyLocationAddress()でアクセスできるようにします。 –

関連する問題