【问题标题】:Loading GeoPoints / Path on MapView is taking forever. Any way to speed it up?在 MapView 上加载 GeoPoints / Path 需要很长时间。有什么办法可以加快速度?
【发布时间】:2011-11-16 17:48:01
【问题描述】:

我正在为应用程序使用自定义 MapView。我有一个保存了很多 GPS 数据点的数据库,我目前正在使用 Overlays 加载它们,然后使用 Overlays 在 MapView 上执行 .addAll。

但是,将几百个数据点作为 MapView 上的路径加载最多需要 2 分钟。

加载数据点的代码是:

    private void loadMap() {        
    //Load the specific data from the database
    DBAdapter db = new DBAdapter(this);
    try {
        db.open();
        Cursor mDataPoints = db.db.rawQuery("SELECT * FROM GPS_DATA)", null);
        if ((mDataPoints != null) && (!mDataPoints.isClosed()) && (mDataPoints.moveToFirst())) {                
            mapMain.setFinishedLoading(false);
            mapMain.getOverlays().clear();

            if ((!mDataPoints.isClosed()) && (mDataPoints.moveToFirst())) {
                while (!mDataPoints.isAfterLast()) {

                    this.drawPath(db, 
                                  DBAdapter.formatLong(mDataPoints, "data_id"), 
                                  DBAdapter.formatString(mDataPoints, "data_name"),
                                  DBAdapter.formatString(mDataPoints, "data_type"));                        

                    mDataPoints.moveToNext();
                }
            }

            mapMain.refreshDrawableState();
            mapMain.setEnabled(true);
        }
    } finally {
        db.close();
    }
}

public void drawPath(DBAdapter db, long pDataID, String pDataName, String pDataType) {  

    Cursor mData = db.getPointDataByDataID(pDataID);
    if ((!mData.isClosed()) && (mData.moveToFirst())) {
        boolean vFirst = true;
        int i = 0;
        GeoPoint startGP;
        GeoPoint geoPoint1;
        GeoPoint geoPoint2 = null;

        while (!mData.isAfterLast()) {
            if (vFirst) {
                startGP = new GeoPoint((int) (DBAdapter.formatDouble(mData, "latitude") * 1E6), (int) (DBAdapter.formatDouble(mData, "longitude") * 1E6));
                mapMain.getOverlays().add(new Map_Route_Overlay(startGP, startGP, this.returnPathColor(pDataType), pDataName));
                vFirst = false;
                geoPoint2 = startGP;
            } else {
                geoPoint1 = geoPoint2;

                geoPoint2 = new GeoPoint((int) (DBAdapter.formatDouble(mData, "latitude") * 1E6), (int) (DBAdapter.formatDouble(mData, "longitude") * 1E6));
                if (geoPoint2.getLatitudeE6() != 22200000) {
                    mapMain.getOverlays().add(new Map_Route_Overlay(geoPoint1, geoPoint2, this.returnPathColor(pDataType), pDataName));
                }
            }

            i += 1;
            mData.moveToNext();
        }

        if (geoPoint2 != null) { 
            mapMain.getOverlays().add(new Map_Route_Overlay(geoPoint2, geoPoint2, this.returnDataColor(pDataType), pDataName));
        }
    }
    mData.close();
}
public int returnDataColor(String pType) {
    int vReturnData = Color.BLUE;

    if (pType.trim().equals("S")) {
        vReturnData = Color.RED;
    }

    return vReturnData;
}

Map_Route_Overlay 类的代码是:

public final class Map_Route_Overlay extends Overlay {
  private static String TAG = CoreFunctions.APP_TAG + "_MapRouteOverlay";

  private GeoPoint geoPoint1;
  private GeoPoint geoPoint2;
  private int defaultColor = 999;
  private String dataName = "";

  public Map_Route_Overlay(GeoPoint pGeoPoint1, GeoPoint pGeoPoint2, int pColor, String pText) {
    this.geoPoint1 = pGeoPoint1;
    this.geoPoint2 = pGeoPoint2;
    this.defaultColor = pColor;
    this.dataName = pText;
  }

  public void setDataName(String pName) {
    this.dataName = pName;
  }

  @Override
  public boolean draw (Canvas canvas, MMMapView mapView, boolean shadow, long when) {
    Projection projection = mapView.getProjection();
    if (shadow == false) {
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        Point point = new Point();
        projection.toPixels(this.geoPoint1, point);
        paint.setColor(this.defaultColor);
        Point point2 = new Point();
        projection.toPixels(this.geoPoint2, point2);
        paint.setStrokeWidth(2);
        paint.setAlpha(this.defaultColor == Color.parseColor("#6C8715")?220:120);
        canvas.drawLine(point.x, point.y, point2.x,point2.y, paint);
    }

    return super.draw(canvas, mapView, shadow, when);
  }

}

我尝试了几个选项,包括使用线程加载数据,但通知 MapView 在完成时加载叠加层已成为一场噩梦。

仅供参考,MMMapView是我自定义的MapView组件,目前只是继承了MapView,实现了两个功能。

【问题讨论】:

    标签: android google-maps overlay android-mapview itemizedoverlay


    【解决方案1】:

    您真的不应该尝试同时在屏幕上显示数百个叠加层,因为您的性能会很差。尝试将其限制为 10-20。除此之外,它对用户来说太忙了。

    您还应该使用线程从数据库中加载项目。我所做的是使用自定义 ItemizedOverlay,并在线程中添加自定义 OverlayItems。然后,一旦我完成加载,我就会调用 mapView.postInvalidate(); 以便它们显示在屏幕上。

    【讨论】:

    • 我认为尝试加载这么多叠加层会导致严重的性能问题。我想要完成的是从我在数据库中的数据中绘制一堆路径。路径由唯一 id 分解,然后在另一个数据库表中具有相应的记录,可能在 10 个 GeoPoints 或 200 个 GeoPoints 之间(取决于路径的长度)。
    • 我不确定这是否会有所作为,但你能不能只用一个 Overlay 来表示并绘制所有路径?它可能会产生性能差异
    【解决方案2】:

    我正在使用这个:http://code.google.com/p/mapview-overlay-manager/

    它提供 LazyLoading 以仅加载地图上的可见标记。

    【讨论】:

    • 有人有这个库的经验吗?性能稳定吗?
    • 我仅将它用于我的个人小型项目,但测试了 1000 个标记放置在 1 个叠加层上并且没问题。我没有任何不同方法的经验,所以不知道它在其他方法中的表现如何。
    • 试过了,但不是很灵活。
    猜你喜欢
    • 2012-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-03
    相关资源
    最近更新 更多