android - MapsForge动态添加的覆盖项目不会重绘

标签 android android-mapview openstreetmap

我想在运行时向 MapsForge 添加一些 Overlay 项目;我可以成功添加它们,但它们仅在我移动或缩放 map 后才会出现。实际上应该如此,因为每次移动或缩放时,MapsForge 都会通过 invalidate() 函数设法重新绘制图 block 和覆盖项目。无论如何,as it is stated here ,这是自 MapsForge 版本 0.2.0 以来已修复的问题:MapView 类应该能够在添加新项目时自动重绘它们。

我正在使用 MapsForge 0.3.1。要么我做错了什么,要么这个问题在当前版本中再次出现。这是我的代码;我重用了 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
    }
}

最佳答案

在添加覆盖项后尝试添加覆盖 Controller

In fact i found out that you can just use another function instead.

示例:

mapView.getOverlays().add(listOverlay);
//redraw once overlay items have been added/removed
mapView.getOverlayController().redrawOverlays(); //method 1
/**OR**/
mapView.redraw(); //method 2

希望这有帮助!

关于android - MapsForge动态添加的覆盖项目不会重绘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13992374/

相关文章:

java - 在 Android 中单击一个按钮即可打开 Google map !

sql - PostgreSQL 9.6 中窗口函数的不稳定查询行为

java - 有没有一种方法可以获取边所属方式的 ID?

java - 如何在java中将多个多边形合并为一个

android - Intent 过滤器在前台 Activity 中覆盖

android - 关于 SMS Retriever API 的哈希键

android - 使用 Intent 在第二个 Activity 中使用 putExtra 和 getExtra 方法接收空数据

php - 通过 mysql 进行 JSON Android 登录验证

android - 将 MapFragment 与 min sdk-8 一起使用

java - 打开街道 map (osmdroid) 在 PC 中显示灰色瓷砖而不是 map