'Osmdroid: How to force a map to refresh?
I am displaying a large number of overlays on a map; the process takes 5-10 seconds. Rather than using a progress bar I want the overlays to appear gradually on the map over this time. So for every 25 lines that are calculated I am trying to show them before continuing:
for (KmlRoad road :roads)
{
// 25 lines in this array
Polyline pline = new Polyline();
pline.getOutlinePaint().setColor(Color.RED);
pline.setPoints(road.points);
map.getOverlayManager().add(pline);
++roadsShown;
}
map.invalidate();
progressText.setText(roadsShown + " roads");
But nothing happens for a while and then all the overlays (several hundred of them) appear at once. The 'progressText' isn't updated, either.
I have also tried doing the calculation part in an async task, and sending an intent to the map activity for every 25 lines. It gives the same result, unless I add
SystemClock.sleep(xx);
to to async task, which seems very crude and is only guessing at how long the lines take to draw. Here's the code in the async task, which is called every time 25 lines have been calculated:
private void sendRoads(ArrayList<KmlRoad> roads25)
{
ArrayList<KmlRoad> theseRoads = new ArrayList<>(roads25);
// prepare array for next batch while we deal with this one
roads25.clear();
Intent intent = new Intent("newRoads");
intent.putParcelableArrayListExtra("newRoads", theseRoads);
LocalBroadcastManager.getInstance(activity).sendBroadcast(intent);
// procSpped is a crude assessment of how slow the device runs,
// generated by doing a repeat loop of math functions
SystemClock.sleep(2 * activity.procSpeed);
}
... and (if using the async method) the receiver in the main activity, which surrounds the line-drawing code above:
private final BroadcastReceiver onNewRoad = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
ArrayList<KmlRoad> roads = intent.getParcelableArrayListExtra("newRoads");
....
....
}
};
What can I do to make this show as intended?.
Solution 1:[1]
Its rather easy, also its just a work around....
get the current center of your map like
// MyMap is the osmdroid.MapView in the AndroidActivity
GeoPoint center = this.MyMap.getMapCenter();
and now set this center to map:
this.MyMap.getController().setCenter(center);
this forces your Map to refresh, like a manual movement would do the same
BestRegards
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Projekt Check |