2012-07-24 22 views
0

我想獲取用戶位置使用GPS,但我需要轉換lat和long來解決,但geocoder不斷給出錯誤gc是未定義的。地理編碼器未定義?

public class MyLocationListener implements LocationListener{ 

      public void onLocationChanged(Location loc){ 

       try{ 
       Geocoder gc = new Geocoder(this, Locale.ENGLISH); 
       List <Address> addr = gc.getFromLocation(loc.getLatitude(),loc.getLongitude(),1); 
       if (addr != null && addr.size() > 0){ 
        Address address = addr.get(0); 
        String results = address.getAddressLine(0); 
        Toast.makeText(getApplicationContext(),results,Toast.LENGTH_LONG).show(); 
       } 

       }catch(Exception e){ 

       } 

      } 

回答

1

檢查進口有這個

import android.location.Geocoder; 

而且也

這是指您LocationListener的類。所以把你的activityname.this而不是這個

Geocoder gc = new Geocoder(youractivityname.this, Locale.ENGLISH); 
+0

嗨,已經導入,有沒有在下面的代碼中的錯誤? – 2012-07-24 09:12:21

+0

使用地理編碼器gc = new Geocoder(yourActivityname.this,Locale.ENGLISH); – Shalini 2012-07-24 09:29:39

+0

您好,我宣佈的結果會返回地址名稱還是街道名稱? – 2012-07-24 10:21:26