0

I'm developing an app which connects to a server to download location data in order to add some markers to a Map (Google Map).

My problem is that the map is setted before I download my data, so it is completly empty (with no markers).

That data download is performed in another Thread by an AsyncTask and I don't know how to set up my map AFTER all data have been gathered.

Some code, just in case it helps:

HttpGetWorker.java (in charge of get the data):

...

@Override
protected String doInBackground(String... urls) {
    try {
        return process(urls[0]); //Private method to get data
    } catch (IOException ioe) {
        Log.e("HttpGetWorker:", ioe.getLocalizedMessage(), ioe);
        return "Unable to retrieve web page. URL may be invalid.";
    }
}

@Override
protected void onPostExecute(String result) {
    for(AsyncTaskListener<String> listener : listeners) {
        /**
         * Activities which need the server data have to be listeners
         * of this task. this processResult method comes from an own
         * interface(AsyncTaskListener) and it is implemented in my "main"
         * activity (see below)
         */
        listener.processResult(result); 
    }

    progressDialog.dismiss(); //just a progress dialog set in onPreExecute method
}

...

Then the listener of this task, my "main" activity MapsActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);

    initData(); //Here it is where I get the server Data, complete method below

    setUpMapIfNeeded(); // These method set up my map, but before my data is retrieved,
    mMap.setOnMarkerClickListener(this);
    PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
}

private void initData() {
    /**
     * This HttpDispatcher just creates an instance of the previous 
     * HttpGetWorker with its execute() to download the data. The "this" 
     * argument is refered to the current activity to register it in my 
     * listeners list, as said before
     */

    HttpDispatcher dispatcher = new HttpDispatcher(this);
    dispatcher.doGet(this);
}

...

// Interface method, here my net data is process
@Override
public void processResult(String result) {
    //stuff to process the result, it does it well, but too late.
}

Hope I explain myself.

Thanks everyone!

EDIT: Relating to the specific problem, sometimes the easiest solutions are the hardest to see...or my head was just a mess when I posted this. I just change an addMarkers() method to my proccessResult(String s), after all data have been fetched...

Anyway, I think the question itself could be interesting for others (synchronization between an AsyncTask and any other activity which calls it). So I let it here.

Cheers!

3
  • 1
    I don't understand why you want to fetch your data before the map shows up. You can absolutely add a marker once the call has been finished: developers.google.com/maps/documentation/android-api/marker
    – Sw0ut
    Jan 4, 2016 at 19:07
  • @Sw0ut yeap...just that easy, my head was a mess when I posted that and was completely stucked; anyway it could be interesting to propose som solution about the general problem: synchronization between an AsyncTask and any other activity which calls it. Thanks! Jan 4, 2016 at 19:29
  • I think it has no sense to wait for something in the UI thread before launching an AsyncTask. Anyway I don't have the solution. Good luck with your project :)
    – Sw0ut
    Jan 5, 2016 at 8:23

1 Answer 1

0

Don't set the marker before is AsyncTask finished. When task is finished, set marker from onPostExecute().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.