2011-04-29 5 views
2

다음 코드를 사용하여 지오 코더를 정의되지 않은 상태로 유지합니다. 나는 단순히 위도와 경도의 장소 주소를 얻으려고 노력하고있다. 지오 코더 지오 코더 선 = 새 지오 코더 (this, Locale.ENGLISH); 항상 undifined로 되돌아옵니다. 이 정의되지 않은 때문이다생성자 Geocoder()가 정의되지 않았습니다.

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    /* Use the LocationManager class to obtain GPS locations */ 
    LocationManager mlocManager = 
    (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    LocationListener mlocListener = new MyLocationListener(); 
    mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 600000, 1000, mlocListener); 

/* Class My Location Listener */ 
public class MyLocationListener implements LocationListener 
{ 
@Override 
public void onLocationChanged(Location loc) 
{ 
loc.getLatitude(); 
loc.getLongitude(); 
Geocoder geocoder = new Geocoder(this, Locale.ENGLISH); 
currentLatitude = loc.getLatitude(); 
currentLongitude = loc.getLongitude(); 

try { 
     List<Address> addresses = geocoder.getFromLocation(currentLatitude, currentLongitude, 1); 

     if(addresses != null) { 
     Address returnedAddress = addresses.get(0); 
     StringBuilder strReturnedAddress = new StringBuilder("Address:\n"); 
     for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) { 
     strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n"); 
     } 
     myAddress.setText(strReturnedAddress.toString()); 
     } 
     else{ 
     myAddress.setText("No Address returned!"); 
     } 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     myAddress.setText("Canont get Address!"); 
    } 

답변

1

: http://developer.android.com/reference/android/location/Geocoder.html 당신에게이 생성자를 보여줍니다. 하나는 컨텍스트를 취하고, 다른 하나는 컨텍스트와 로케일을 취하지 만, 결정적으로는 MyLocationListener 클래스 안에 생성합니다. 이것을 사용하십시오 :

Geocoder geocoder = new Geocoder(<ACTIVITY CLASS NAME>.this, Locale.ENGLISH); 

대신에 사용해보십시오.

성능상의 이유로 위치 업데이트를받을 때마다 의 인스턴스를 만들고 MyLocationListener 클래스의 생성자에서 생성하는 것이 좋습니다.

+0

아직도 약간 혼란 스럽지만, 은 내 구체적인 경우에 무엇이 될까요? – 63alfred

+0

예, 훌륭했습니다. 이제 안드로이드에서 쉼표로 구분 된 파일을 읽는 방법을 모색 중입니다. 먼저 응용 프로그램에서 읽을 수 있도록 위치를 알아야합니다. 쉼표로 구분 된 파일의 데이터를 스팅 또는 자바 파일의 배열로 전송할 수 있지만 거기에 쉼표로 구분 된 파일을 놓고 읽는 방법이 있어야합니다. 게다가 쉼표로 구분 된 파일은 텍스트이기 때문에 많은 수의 비트가 아니라 3000 개의 항목과 비슷하지만 많은 입력이 필요합니다. – 63alfred

관련 문제