2012-07-11 71 views
0

我寫了一個簡單的地理編碼應用程序,但它不工作。我給了所有必要的權限。請有人告訴我我要去哪裏出錯。提前預覽。地理編碼示例不能正常工作

public class ForgeocdingActivity extends Activity { 
    Geocoder gc; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     final EditText ed=(EditText)findViewById(R.id.editText1); 
     Button b1=(Button)findViewById(R.id.button1); 
     final String to_add = ed.getText().toString();  
     b1.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) 
      { 
       try 
       { 
       List<Address> address2 = gc.getFromLocationName(to_add,3); 

       if(address2 != null && address2.size() > 0) 
       { 
         double lat1 = address2.get(0).getLatitude(); 
         double lng1 = address2.get(0).getLongitude(); 
         Toast.makeText(getBaseContext(), "Lat:"+lat1+"Lon:"+lng1, Toast.LENGTH_SHORT).show(); 
       }  
       } 
       catch(Exception e) 
       { 
        e.printStackTrace(); 
       } 
      } 
     }); 

    } 
} 
+0

首先,您並未初始化Geocoder對象...請在此處發佈堆棧跟蹤,如果您需要更多幫助,還可以提高您的接受評級:) – Cata 2012-07-11 10:38:56

+0

您是在真實設備上還是在仿真器上測試您的?因爲'Geocoder'不能在模擬器上工作 – 2012-07-11 11:02:52

+0

@AditiSharma請看我的答案。 – 2012-07-11 11:48:27

回答

1

請填寫下面的代碼即

Geocoder geocoder = new Geocoder(this, Locale.getDefault()); 
List<Address> addresses = geocoder.getFromLocation(currentlat, currentlng, 1); 

現在地址列表包含最近的已知區域並將此代碼測試到實際設備中。

0

下面是地址解析的工作示例:

Geocoder geocoder = new Geocoder(this, Locale.getDefault()); 
    try { 
     List<Address> list = geocoder.getFromLocation(lat,long, 1); 
     String address = list.get(0).getAddressLine(0) + ", " + list.get(0).getAddressLine(1) + ", " + list.get(0).getAddressLine(2);    
    } catch (IOException e) { 
    e.printStackTrace(); 
    } 

在哪裏 「這個」=活動中, 「長」=經度, 「LAT」=緯度

0

我認爲你可以嘗試從address1獲取緯度和經度。那麼您將從以下代碼獲得經緯度 列表地址= geocoder.getFromLocation(LATITUDE,LONGITUDE,1);