I'm trying to get the positions from my WS and update my markers in my GoogleMap fragment so what I'm doing is:
I've my HomeActivity which contains 2 fragments (2 GoogleMaps where one have TileOverlay).
In my GoogleMap fragment I'm trying to obtain my Markers Positions from OnCameraChangeListener so the markers are added as the user moves on.
I'm using EventBus and Okhttp for an async request!
My GoogleMapFragment:
public class GoogleMapFragment extends FragmentBase {
@Override
public void onResume() {
mapView.onResume();
BusHelper.register(this); // Register the EventBus with my helper
super.onResume();
}
@Override
public void onPause() {
BusHelper.unregister(this); // Unregister the EventBus with my helper
super.onPause();
}
public GoogleMap.OnCameraChangeListener getCameraChangeListener() {
return new GoogleMap.OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition cameraPosition) {
//those are corners of visible region
VisibleRegion visibleRegion = mMap.getProjection().getVisibleRegion();
LatLng farLeft = visibleRegion.farLeft;
LatLng farRight = visibleRegion.farRight;
LatLng nearLeft = visibleRegion.nearLeft;
LatLng nearRight = visibleRegion.nearRight;
double minY = nearLeft.latitude;
double minX = nearLeft.longitude;
double maxY = farRight.latitude;
double maxX = farRight.longitude;
//Send the WS Request to a manager who uses okhttp
ApiManager.getPositions(minX, minY, maxX, maxY);
}
};
}
//Receive the eventBus event
public void onEvent(GetPositionsEvent event) {
if (event.positions != null)
setMarkersByVisibleRegion(event.positions);
}
After this it will do the WS Request in APIManager.
public class IMSApiManager {
private final static Gson gson = new Gson();
private static Positions mPositions;
/**
* @return Positions
*/
public static void getPositions(double minX, double minY, double maxX, double maxY) {
String TAG = "getPositions";
String wSRequest = AppConstants.REQUEST_POSITIONS
.replace("{vUser}", "CARREPH1")
.replace("{vMinX}", new BigDecimal(minX).toPlainString())
.replace("{vMinY}", new BigDecimal(minY).toPlainString())
.replace("{vMaxX}", new BigDecimal(maxX).toPlainString())
.replace("{vMaxY}", new BigDecimal(maxY).toPlainString());
try {
RestAsyncHttpClient.doGetRequest(wSRequest, new GetPositionsCallback() {
@Override
public void onFailure(Request request, IOException e) {
super.onFailure(request, e);
}
@Override
public void onResponse(Response response) throws IOException {
super.onResponse(response);
mPositions = gson.fromJson(response.body().charStream(), Positions.class);
BusHelper.post(new GetPositionsEvent(mPositions)); //Post the eventBus
}
});
} catch (IOException exp) {
LogHelper.error(TAG, "Error getting positions", exp);
}
}
}
I'm familiar with Not on the mainThread error, but in theory this will be possible, if not how i can add markers without doing a new instance of the fragment.
Just use onEventMainThread instead onEvent. It solved my problems too.
Why? Because onEvent is called in the same thread, you need run your code on MainThread.
You can call post on eventbus's object (when background thread is successful). The argument of post is an object of any class, so design a custom class as per your requirement which will hold the result from the background thread.
Now on the activity (where UI work needs to be done), simply define :
CobbocEvent is your custom class for storing background thread's result data.:
I am sure you get the picture :)
You would get the error when you try to post an object to the EventBus in background thread. The solution is, if post(...) is called in background thread, let Handler move it to main thread.
Override your BusHelper's
post(...)
method and do the following.Hope this helps.
Accepted answer only works for versions lower than third, for other versions you should use threadMode = ThreadMode.MAIN (documentation)