问题描述
我正在尝试从WS获取位置并更新GoogleMap片段中的标记,所以我要做的是:
I'm trying to get the positions from my WS and update my markers in my GoogleMap fragment so what I'm doing is:
我有HomeActivity,其中包含2个片段(2个GoogleMap,其中一个具有TileOverlay).
I've my HomeActivity which contains 2 fragments (2 GoogleMaps where one have TileOverlay).
在我的GoogleMap片段中,我试图从OnCameraChangeListener获取标记位置,以便在用户继续前进时添加标记.
In my GoogleMap fragment I'm trying to obtain my Markers Positions from OnCameraChangeListener so the markers are added as the user moves on.
我正在使用EventBus和Okhttp进行异步请求!
I'm using EventBus and Okhttp for an async request!
我的GoogleMapFragment:
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);
}
此后,它将在APIManager中执行WS Request.
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);
}
}
}
我熟悉Not on the mainThread错误,但是从理论上讲这是可能的,如果没有的话,我可以在不执行该片段的新实例的情况下添加标记.
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.
推荐答案
只需使用onEventMainThread代替onEvent.它也解决了我的问题.
Just use onEventMainThread instead onEvent. It solved my problems too.
// Called in Android UI's main thread
public void onEventMainThread(MessageEvent event) {
textField.setText(event.message);
}
为什么?由于onEvent是在同一线程中调用的,因此您需要在MainThread上运行代码.
Why?Because onEvent is called in the same thread, you need run your code on MainThread.
这篇关于EventBus不在主线程上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!