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.
Accepted answer only works for versions lower than third, for other versions you should use threadMode = ThreadMode.MAIN (documentation)
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventMainThread(MessageEvent event) {
textField.setText(event.message);
}