Issue
I wanted to add some Overlay items to MapsForge at runtime; I can successfully add them, but they appear only after i move or zoom the map. As it should actually be, since with every moving or zooming, MapsForge manage to redraw the tiles and the overlaying items through the invalidate() function. Anyway, as it is stated here, this is an issue already fixed since MapsForge release 0.2.0: MapView Class should automatically be able to redraw new items whenever they're added.
I am using MapsForge 0.3.1. Either I am doing something wrong, or this issue reappeared in current release. Here is my code; I reused some code from the official MapsForge Sample activities:
public class MyMapView extends MapView{
//I brought some variables outside the Sample's function in order to add new items dynamically to the lists
ListOverlay mListOverlay;
List<OverlayItem> mOverlayItems;
Polyline mPolyline;
Circle mLastPosition;
//Basically here I do the same actions shown in the MapsForge samples. These items appear at once, with the onCreate() functions of the Activity
public void addInitialOverlays(){
Circle circle = createCircle(CENTRAL_STATION);
Polygon polygon = createPolygon(Arrays.asList(VICTORY_COLUMN, CENTRAL_STATION, BRANDENBURG_GATE));
mPolyline = createPolyline(Arrays.asList(BRANDENBURG_GATE, VICTORY_COLUMN));
Marker marker1 = createMarker(R.drawable.marker_red, VICTORY_COLUMN);
Marker marker2 = createMarker(R.drawable.marker_green, BRANDENBURG_GATE);
mLastPosition = createCircle(VICTORY_COLUMN);
mOverlayItems.add(circle);
mOverlayItems.add(polygon);
mOverlayItems.add(mPolyline);
mOverlayItems.add(marker1);
mOverlayItems.add(marker2);
mOverlayItems.add(mLastPosition);
getOverlays().add(mListOverlay);
}
//Here is the relevant part. This is called periodically from the main Activity
public void addPointToRoute(double latitude, double longitude) {
GeoPoint newPoint = new GeoPoint(latitude, longitude);
//Tried both removing the list from the overlays and re-adding it, and leaving it where it is; same result
//getOverlays().remove(mListOverlay);
mOverlayItems.remove(mPolyline);
List<GeoPoint> oldPolyline = mPolyline.getPolygonalChain().getGeoPoints();
oldPolyline.add(newPoint);
mPolyline = createPolyline(oldPolyline);
mOverlayItems.add(mPolyline);
mLastPosition.setGeoPoint(newPoint);
//getOverlays().add(mListOverlay);
invalidateOnUiThread();//This MapView function calls either invalidate() or postInvalidate(), depending on the situation
}
}
Solution
try adding the overlaycontroller after adding the overlay items
In fact i found out that you can just use another function instead.
example:
mapView.getOverlays().add(listOverlay);
//redraw once overlay items have been added/removed
mapView.getOverlayController().redrawOverlays(); //method 1
/**OR**/
mapView.redraw(); //method 2
hope this helps!
Answered By - fyptum
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.