Reputation: 309
I would like to change the Position of the map in google maps v2
But Ive done it in a TimerTask ... target, zoom, bearing and so on and it says
"IllegalStateException - not on the main thread
What should I do? Any help?
class Task extends TimerTask {
@Override
public void run() {
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(Zt) // Sets the center of the map to Mountain View
.zoom(12) // Sets the zoom
.bearing(180) // Sets the orientation of the camera to east
.tilt(30) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
mMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
}
Timer timer = new Timer();
timer.scheduleAtFixedRate(new Task(), 0, 20000);
Upvotes: 2
Views: 534
Reputation: 1803
The timer task runs on a different thread than the UI thread. In Android, you are not allowed to perform UI operation from a non-UI thread. Use runOnUiThread
method to send operations to the UI thread:
class Task extends TimerTask {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(Zt) // Sets the center of the map to Mountain View
.zoom(12) // Sets the zoom
.bearing(180) // Sets the orientation of the camera to east
.tilt(30) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
mMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
});
}
}
Timer timer = new Timer();
timer.scheduleAtFixedRate(new Task(), 0, 20000);
Upvotes: 0