开发者

Google Map Lag on Android

开发者 https://www.devze.com 2023-03-23 16:11 出处:网络
I\'ve been developing an app that reads a .csv file, takes out approximately 16 latitude/longitude points, and plots them on a Google Map.

I've been developing an app that reads a .csv file, takes out approximately 16 latitude/longitude points, and plots them on a Google Map.

However, it seems that the points don't like to plot until I touch the screen after about a minute (I have no listeners established) [this is a separate problem that maybe someone can answer as well].

The task of getting the latitude/longitude points and plotting them is put into an AsyncTask doInBackground method. Refreshing the map's drawable state is done in the AsyncTask's onPostExecute method.

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();
    }

}

}


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


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.

first get all the lat/lng and store in p99 array and then add the mapoverlay

implement code

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();
    }

your draw()

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;
} 
0

精彩评论

暂无评论...
验证码 换一张
取 消