1

텍스트 상자의 텍스트를 설정하기 위해 getMyLocationAddress의 onLocationChanged 메소드에있는 현재 위치에 액세스하려고합니다. 정확한 변수가 저장되어있어 geocoder.getLocationFrom()에서 사용할 수있는 변수를 알고 싶습니다.getMyLocationAddress 메소드의 onLocationChanged에서 현재 위치에 어떻게 액세스합니까?

geocoder.getFromLocation (latLng.getLongitude(), 1);에서 LatLng 객체 latLng을 사용하고 있습니다. 내가 어디에있는 것 같아요 현재 위치 stored.But getFromLocation 위치 개체가 필요합니다. 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

네하지만있는 위치 변수는 내 현재 위치는 latLng를 변수가 지역 variable.it 만 onLocationChanged() 메소드에 액세스 할 수 있습니다 –

+0

저장됩니다

여기에서 자세한 내용을보실 수 있습니다. getMyLocationAddress()에서 액세스 할 수 있도록 전역 적으로 선언하십시오. –