0

In my Maps Activity, I have an AsyncTask that communicates using sockets with another program and gets some info. According to that info, I have to move certain markers on my map. How will that be possible?

If I simply try to use setPosition() directly from the backround task, it doesn't work.

The movement of the markers per say doesn't concern me, all the necessary Lists, MarkerOptions, etc will be added. The issue is WHERE to do them so that the map gets updated and i can see the markers moving (puting them in onMapReady doesn't work. The function has to end in order for the map to show up).

Here is the backround task:


class backTask extends AsyncTask<Void, Void, Void>
    {
        Consumer consumer;

        public backTask(Consumer c)
        {
            this.consumer = c;
        }

        @Override
        protected Void doInBackground(Void... voids)
        {
            String comm_info = consumer.Start();

            StringTokenizer tok = new StringTokenizer(comm_info, "+");
            int bro_port = Integer.parseInt(tok.nextToken());
            String bro_ip = tok.nextToken();

            //keep asking for updates
            Message question = new Message("update?");
            Message answer;

            try
            {
                while(true)
                {
                    Socket Con_sock = new Socket(InetAddress.getByName(bro_ip), bro_port);
                    ObjectOutputStream ask = new ObjectOutputStream(Con_sock.getOutputStream());
                    ObjectInputStream rep = new ObjectInputStream(Con_sock.getInputStream());


                    //asking
                    ask.writeObject(question);
                    ask.flush();

                    //get response
                    answer = (Message) rep.readObject();
                    if (answer.type == 2)
                    {
                        //No update
                    }
                    else if (answer.type == 0)
                    {
                        //got update
                        //move / create markers here
                    }
                    Thread.sleep(500);
                }//loop
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return null;
        }

    }//backTask END
}

And the onMapReady method:

public void onMapReady(GoogleMap googleMap)
    {
        mMap = googleMap;

        LatLng athens = new LatLng(37.93, 23.731960);
        mMap.moveCamera(CameraUpdateFactory.newLatLng(athens));
        mMap.animateCamera(CameraUpdateFactory.zoomTo(13));

        Consumer con = (Consumer)getIntent().getExtras().get("Consumer");
        backTask BT = new backTask(con);
        BT.execute();
}

  • `onPostExecute` runs on ui thread and at that point you can display your results. if an intermediate result then `onProgressUpdate` is available and is also on ui thread. Lastly, you can always use `runOnUiThread` from `doInBackground` but that is viewed as an anti-pattern to the asyncTask (short duration work in background). If you post applicable code I can propose a specific solution. –  May 28 '19 at 16:24
  • Thanks for replying Andy! I have added some code as well as some extra explanation. I will give the things you suggested a shot, although some further guidance would be much appreciated. – Μιχάλης Χρηστίδης May 29 '19 at 11:29
  • Simplest approach is to use `onProgressUpdate` and you can use the 2nd parameter in the asynctask generic to specify the type of data - this handles any contention issues if you were to just use a field variable. The timing is undefined but from the looks of it as long as it moves the marker in a relatively timely fashion it would be OK. I'd include code but the Q is on hold. Here's a simple example in an answer: https://stackoverflow.com/a/6450407/2711811 –  May 29 '19 at 11:38
  • Seems like it's working! Thank you very much! – Μιχάλης Χρηστίδης May 29 '19 at 12:04

0 Answers0