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