2014-11-21 143 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(..)中使用的类数据成员。