Reputation: 21
I am trying to update the marker
with this code:
// create marker
MarkerOptions marker1 = new MarkerOptions().position(new LatLng(latitude, longitude)).rotation(head);
// Changing marker icon
marker1.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher));
// adding marker
googleMap.addMarker(marker1);
CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(latitude, longitude)).zoom(17)
.build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
It is working fine when I am using this on main thread but It's not working when I am calling from Handler
.
Upvotes: 2
Views: 1267
Reputation: 4950
Yes, you can update your User Interface using handler
. Create a separate method that fetch and display marker on map. A handler allows you to send and process Message
and Runnable objects associated with a thread's MessageQueue.
There are two main uses for a Handler:
To enqueue an action to be performed on a different thread than your own.
Handler UI_HANDLER = new Handler();
UI_HANDLER.postDelayed(UI_UPDTAE_RUNNABLE, 30000);
Below is Runnable method put any where in your activity.
Runnable UI_UPDTAE_RUNNABLE = new Runnable() {
@Override
public void run() {
drawAllMarker();//Method that will get employee location and draw it on map
UI_HANDLER.postDelayed(UI_UPDTAE_RUNNABLE, 30000);
}
};
Upvotes: 1