GARVIT ARORA
GARVIT ARORA

Reputation: 21

How to update marker from handler on google map android?

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

Answers (1)

Android Enthusiast
Android Enthusiast

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 schedule messages and runnables to be executed as some point in the future.
  • 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

Related Questions