1

我一直在开发一个读取 .csv 文件的应用程序,取出大约 16 个纬度/经度点,并将它们绘制在谷歌地图上。

但是,似乎这些点不喜欢绘制,直到我在大约一分钟后触摸屏幕(我没有建立听众)[这是一个单独的问题,也许有人也可以回答]。

获取纬度/经度点并绘制它们的任务被放入 AsyncTask doInBackground 方法中。刷新地图的可绘制状态是在 AsyncTask 的 onPostExecute 方法中完成的。

I figured that I would eliminate lag since I have the AsyncTask method working for me. When the program starts, it shows a blank map, I wait about 15 seconds and, if I touch the map, the points will plug in. However, the map is unbearably laggy at this point! It takes the app at least 5 seconds to respond to my interaction (i.e. zoom, scroll), and even then it does the action really slow...

Does anything think they know the cause of this?

Here's most of my code:

package net.learn2develop.GoogleMaps;

-- imports here


public class MapsActivity extends MapActivity 
{    
MapView mapView; 
MapController mc;
//GeoPoint p;
//GeoPoint p2;
GeoPoint[] p99 = new GeoPoint[16];
public static String[][] bump = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mapView = (MapView) findViewById(R.id.mapView);
    LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);  
    @SuppressWarnings("deprecation")
    View zoomView = mapView.getZoomControls(); 

    mapView.setStreetView(true);
    mapView.setSatellite(false);

    zoomLayout.addView(zoomView, 
        new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, 
            LayoutParams.WRAP_CONTENT)); 
    mapView.displayZoomControls(true);

    mc = mapView.getController();


    // Add points from ReadCsv.java

    /** try/catch to-> async was cut from here */
    new MapPoint().execute(bump);


    mapView.invalidate();
}

public String[][] getArray(BufferedReader bufRdr) {
          -- my method that I know works
}


@Override
protected boolean isRouteDisplayed() {
    return false;
}

class MapOverlay extends com.google.android.maps.Overlay {
    @Override
    public boolean draw(Canvas canvas, MapView mapView, 
            boolean shadow, long when) 
    {
        super.draw(canvas, mapView, shadow);                   

        // -- array of points---
        for(int i = 0; i < bump.length; i++) {
            Point screenPts99 = new Point();
            mapView.getProjection().toPixels(p99[i], screenPts99);
            Bitmap bmp99 = BitmapFactory.decodeResource(
                    getResources(), R.drawable.redpin);
            canvas.drawBitmap(bmp99, screenPts99.x, screenPts99.y-44, null);
        }

        return true;
    } 
}

private class MapPoint extends AsyncTask <String[][], String, String> {

    @Override
    protected String doInBackground(String[][]... voidThisArray) {
        String voidThisString = null;
        try {
            InputStream is = getAssets().open("Training4.csv");
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            bump = getArray(reader);
            if(bump == null){
                setContentView(R.layout.deleteme);
            } else {
                for(int i = 0; i < bump.length; i++) {
                    String coordinates99[] = {bump[i][0], bump[i][1]};
                    double lat99 = Double.parseDouble(coordinates99[0]);
                    double lng99 = Double.parseDouble(coordinates99[1]);
                    p99[i] = new GeoPoint(
                            (int) (lat99 * 1E6),
                            (int) (lng99 * 1E6));
                    MapOverlay mapOverlay99 = new MapOverlay();
                    List<Overlay> listOfOverlays99 = mapView.getOverlays();
                    listOfOverlays99.add(mapOverlay99);
                    mapView.refreshDrawableState();
                } 
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return voidThisString;
    }

    @Override
    protected void onPostExecute(String voidThisString) {
        super.onPostExecute(voidThisString);
        mapView.refreshDrawableState();
        mapView.invalidate();
    }

}

}
4

2 回答 2

1

in onPostExecute use the mapView.invalidate(); method it'll refresh it and whatever you done in inBackground() that will be refreshing by this method

于 2011-07-29T14:01:32.817 回答
1

you add the MapOverlay in for loop so what happen each overlay contain bump.length points. so it will getting slow add dupplication of point.

首先获取所有 lat/lng 并存储在 p99 数组中,然后添加 mapoverlay

实现代码

try {
        InputStream is = getAssets().open("Training4.csv");
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        bump = getArray(reader);
        if(bump == null){
            setContentView(R.layout.deleteme);
        } else {
            for(int i = 0; i < bump.length; i++) {
                String coordinates99[] = {bump[i][0], bump[i][1]};
                double lat99 = Double.parseDouble(coordinates99[0]);
                double lng99 = Double.parseDouble(coordinates99[1]);
                p99[i] = new GeoPoint(
                        (int) (lat99 * 1E6),
                        (int) (lng99 * 1E6));
            } 
                MapOverlay mapOverlay99 = new MapOverlay();
                List<Overlay> listOfOverlays99 = mapView.getOverlays();
                listOfOverlays99.add(mapOverlay99);
                mapView.refreshDrawableState();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

你的平局()

public boolean draw(Canvas canvas, MapView mapView, 
        boolean shadow, long when) 
{
    super.draw(canvas, mapView, shadow);                   

    // -- array of points---
    if(bump!=null){
      for(int i = 0; i < bump.length; i++) {
        Point screenPts99 = new Point();
        mapView.getProjection().toPixels(p99[i], screenPts99);
        Bitmap bmp99 = BitmapFactory.decodeResource(
                getResources(), R.drawable.redpin);
        canvas.drawBitmap(bmp99, screenPts99.x, screenPts99.y-44, null);
      }
    }

    return true;
} 
于 2011-07-29T14:38:31.103 回答