2014-11-21 128 views
-1

我嘗試使用AsyncTask反向地理編碼,但通過doInBackground()方法的緯度和經度參數未正確發生。任何想法?如何在doInBackground方法中傳遞參數緯度和經度?

public class SitesAdapter extends ArrayAdapter<StackSite> { 
    public static Double lat; 
    public static Double lng; 

    @Override 
    public View getView(int pos, View convertView, ViewGroup parent){ 
    ... 
     lat = -1.80; 
     lng = -80.20; 
    ... 
    } 
    public void start(){ 
     new GetAddressTask(mContext).execute(lat, lng); 
    } 

    public static class GetAddressTask extends AsyncTask<Double, Void, String> { 
     //mContext    

     @Override 
     protected String doInBackground(Double... params) { 
      Geocoder gc = new Geocoder(mContext, Locale.getDefault());   
      List<Address> list = null; 
      String city = ""; 
      double latitude = params[0]; 
      double longitude = params[1];   
      try { 
       list = gc.getFromLocation(lat, lng, 1);    
      } catch (IOException e) {    
       e.printStackTrace();     
      }    
      if (list != null && list.size() > 0) { 
       Address address = list.get(0); 
       city = String.format("%s, %s", address.getAdminArea(), address.getCountryName());        
      } 
      return city;    
     } 

     @Override 
     protected void onPostExecute(String city) {   
      tituloTxt.setText(city); 
     } 
    } 
} 

錯誤:

11-21 15:10:24.409: E/Trace(24502): error opening trace file: No such file or directory (2) 
+0

基本的Java。你是否熟悉領域,制定者和獲得者? – Preetygeek 2014-11-21 18:02:07

+0

RTFM。它就在那裏: http://developer.android.com/reference/android/os/AsyncTask.html – IuriiO 2014-11-21 18:17:43

+0

您提供的錯誤是無用的。堆棧跟蹤在哪裏?一些有用的信息?你不知道你在做什麼。 – Preetygeek 2014-11-21 20:52:52

回答

1

好了之後所以只有這樣才能通過座標。首先將座標添加到構造函數LatLng(雙緯度,雙倍經度)並傳遞參數。

lat = -1.80; 
    lng = -80.20; 
    LatLng latlng = new LatLng(lat, lng); 
    new GetAddressTask(mContext).execute(lat, lng); 

然後在doInbackground方法裏面獲取參數。

public static class GetAddressTask extends AsyncTask<LatLng, Void, String> { 
    //mContext    

    @Override 
    protected String doInBackground(LatLng... params) { 
     Geocoder gc = new Geocoder(mContext, Locale.getDefault());   
     List<Address> list = null; 
     String city = ""; 
     LatLng loc = params[0]; //Get all parameters: latitude and longitude   
     try { 
      list = gc.getFromLocation(loc.latitude, loc.longitude, 1); //get specific parameters     
     } catch (IOException e) {   
      e.printStackTrace();    
     } 
     if (list != null && list.size() > 0) { 
      Address address = list.get(0); 
      city = String.format("%s, %s", address.getAdminArea(), address.getCountryName()); 
      return city; 
     }else{ 
      return "City no found"; 
     }     
    } 

    @Override 
    protected void onPostExecute(String city) {   
     tituloTxt.setText(city); 
    } 
} 
-1

添加兩個變量到類,並將它們設置在創建異步任務,然後在方法中使用它們。簡單。

public class GetAddressTask extends AsyncTask<String, Void, String> { 
     Context mContext; 
     float lat,lin; 

     public void setLat(int lat){...} 

     //rest of class 

當然,你可以使一切都靜態(領域和setters)。

編輯。

如果您使用某些參數調用execute,請記住在調用execute之前必須設置您的值。

+1

或task.execute(lat,lng); – IuriiO 2014-11-21 18:23:31

+0

我嘗試傳遞座標並執行AsyncTask,但出現錯誤。我上面更新了。 – 2014-11-21 20:36:00

+0

您沒有提供錯誤。 – Preetygeek 2014-11-21 20:45:24

-1

在調用execute方法之前,你可以創建一個構造函數,在這裏你可以初始化你可以在doInBackground(..)中使用的類數據成員。

相關問題