How can I pass parameters Latitude and Longitude in method doInBackground?

1.2k Views Asked by At

I tried to use reverse geocoding with AsyncTask but but the parameters of latitude and longitude that passes through the method doInBackground() are not happening correctly. any idea?

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);
        }
    }
}

error:

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

There are 3 best solutions below

0
On BEST ANSWER

Well after so only had to do this to pass the coordinates. First add coordinate to constructor LatLng(double latitude, double longitude) and pass the parameters.

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

Then inside the doInbackground method get parameters.

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);
    }
}
2
On

Add two variables to your class and set them when you create async task, then use them in method. Simple.

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

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

       //rest of class

of course you can make everything static (fields and setters).

Edit.

If you are calling execute with some parameters, remember that your values have to be set before calling execute.

0
On

before calling execute method, u can just create a constructor ,in which you can initialize your class data members which can be further used in doInBackground(..).