I have separate map class in which I have written all the logic related to map activities as it was a strict requirement to keep both map related things separate. Now from main app activity I am calling function like this:
Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
if (mapObj.isLocationClientConnected)
Location currentLocation = mapObj.gotoCurrentLocation();
}
}, 0, refreshUserLocationInterval);
And in Map Class
I have:
public Location gotoCurrentLocation() {
currentLocation = mLocationClient.getLastLocation();
LatLng ll = new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude());
CameraUpdate cUpdate = CameraUpdateFactory.newLatLngZoom(ll, defaultZoom);
gMap.animateCamera(cUpdate);
return currentLocation;
}
But I get this error:
06-22 19:56:30.900: E/AndroidRuntime(11413): FATAL EXCEPTION: Timer-0
06-22 19:56:30.900: E/AndroidRuntime(11413): java.lang.IllegalStateException: Not on the main thread
06-22 19:56:30.900: E/AndroidRuntime(11413): at kbh.b(Unknown Source)
06-22 19:56:30.900: E/AndroidRuntime(11413): at lzd.b(Unknown Source)
06-22 19:56:30.900: E/AndroidRuntime(11413): at mbi.b(Unknown Source)
06-22 19:56:30.900: E/AndroidRuntime(11413): at fms.onTransact(SourceFile:92)
06-22 19:56:30.900: E/AndroidRuntime(11413): at android.os.Binder.transact(Binder.java:310)
06-22 19:56:30.900: E/AndroidRuntime(11413): at com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a.animateCamera(Unknown Source)
06-22 19:56:30.900: E/AndroidRuntime(11413): at com.google.android.gms.maps.GoogleMap.animateCamera(Unknown Source)
06-22 19:56:30.900: E/AndroidRuntime(11413): at com.mapworlds.mapworlds.MapClass.gotoCurrentLocation(MapClass.java:176)
I want to keep the animateCamera
in same function inside map class. I already have main context from main app available as a variable in this class, can I utilize it and make it work?