(MapView and 1700 Overlays Items).equals(" Slow")

I have a MapView and I'm overlaying 1,700 points onto it, each with the same drawable but with different information. I'm currently using Itemized Overlay to add all the overlays then populate once fished. This works, but the performance is slow. Changing zoom level and focus is jumpy. Now, would it be any better to use ArrayItemizedOverlay since it's the same drawable, or would the map be just as slow?

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.app.Activity;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem; 

public class Points extends ItemizedOverlay <OverlayItem> {
    Context mContext; 
    private ArrayList mOverlays = new ArrayList();

    String newLine = String.format("%n");

    public Points(Drawable defaultMarker) {
        super(boundCenterBottom(defaultMarker));
        // TODO Auto-generated constructor stub
    }

    @Override
    protected OverlayItem createItem(int i) {
        return (OverlayItem) mOverlays.get(i);
    }

    @Override
    public int size() {
        return mOverlays.size();
    }

    @Override
    public void draw(Canvas canvas,
            MapView mapView,
            boolean shadow) {
        if (!shadow)
            super.draw(canvas, mapView, shadow);
    }

    public void addOverlay(OverlayItem overlay) {   
        mOverlays.add(overlay);   
    }

    public void populateNow(){
        setLastFocusedIndex(-1);
        populate();
    }

    public Points(Drawable defaultMarker, Context context) {  
        super(boundCenterBottom(defaultMarker)); 
        mContext = context;
    }

    @Override
    protected boolean onTap(int index) { 
        Intent intent = new Intent();
        OverlayItem item = (OverlayItem) mOverlays.get(index); 
        AlertDialog.Builder dialog1 = new AlertDialog.Builder(mContext);  
        dialog1.setTitle(item.getTitle());  
        String info = item.getSnippet();
        String delims = "[$]";
        String [] tokens = info.split(delims);
        String info1 = tokens [0];
        String info2 = tokens[1];
        String delims2 = "[!]";
        String [] tokens2 = info1.split(delims2);
        double lat = Double.parseDouble(tokens2[0]);
        double lon = Double.parseDouble(tokens2[1]);
        final String location = tokens2[0]+","+tokens2[1];

        dialog1.setMessage(info2);

        dialog1.setPositiveButton("Navigate", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                Nav(location);
            }
        });
        dialog1.setNegativeButton("Directions", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                Direction(location);
            }
        }) ;

        dialog1.show(); 
        return true;
    }

    public void Nav(String location) {
        Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("google.navigation:q=" + location)); 
        mContext.startActivity(i);
    }

    public void Direction(String location) {
        Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?daddr=" + location)); 
        mContext.startActivity(i);
    }
}

How I'm adding items:

mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.plug);
itemizedoverlay = new Points(drawable, this);

while (...) {
    point = new GeoPoint((int) (lat * 1000000), (int) (lon * 1000000));
    overlayitem = new OverlayItem(point, Station_Name, comb);
    itemizedoverlay.addOverlay(overlayitem);
}

This is not a solution, but rather a workaround while waiting for the perfect solution.

I had the same issue and created multiple overlays.

6 overlays with 99 points are really faster than 1 overlay with 600 points.

(15 sec loading time Vs 1 second)

        for (int i = 0; i < 99; i++) {
            webcamCursor.moveToPosition(i);
            float lat = Float.valueOf(webcamCursor.getString(webcamCursor
                    .getColumnIndex("Lat")));
            float lon = Float.valueOf(webcamCursor.getString(webcamCursor
                    .getColumnIndex("Lon")));
            GeoPoint gp = new GeoPoint((int) (lat / 10), (int) (lon / 10));
            myOverlay1.addPoint(gp);
        }
        for (int i = 100; i < 199; i++) {
            webcamCursor.moveToPosition(i);
            float lat = Float.valueOf(webcamCursor.getString(webcamCursor
                    .getColumnIndex("Lat")));
            float lon = Float.valueOf(webcamCursor.getString(webcamCursor
                    .getColumnIndex("Lon")));
            GeoPoint gp = new GeoPoint((int) (lat / 10), (int) (lon / 10));
            myOverlay2.addPoint(gp);
        }
        for (int i = 200; i < 299; i++) {
            webcamCursor.moveToPosition(i);
            float lat = Float.valueOf(webcamCursor.getString(webcamCursor
                    .getColumnIndex("Lat")));
            float lon = Float.valueOf(webcamCursor.getString(webcamCursor
                    .getColumnIndex("Lon")));
            GeoPoint gp = new GeoPoint((int) (lat / 10), (int) (lon / 10));
            myOverlay3.addPoint(gp);
        }
        for (int i = 300; i < 399; i++) {
            webcamCursor.moveToPosition(i);
            float lat = Float.valueOf(webcamCursor.getString(webcamCursor
                    .getColumnIndex("Lat")));
            float lon = Float.valueOf(webcamCursor.getString(webcamCursor
                    .getColumnIndex("Lon")));
            GeoPoint gp = new GeoPoint((int) (lat / 10), (int) (lon / 10));
            myOverlay4.addPoint(gp);
        }
        for (int i = 400; i < 499; i++) {
            webcamCursor.moveToPosition(i);
            float lat = Float.valueOf(webcamCursor.getString(webcamCursor
                    .getColumnIndex("Lat")));
            float lon = Float.valueOf(webcamCursor.getString(webcamCursor
                    .getColumnIndex("Lon")));
            GeoPoint gp = new GeoPoint((int) (lat / 10), (int) (lon / 10));
            myOverlay5.addPoint(gp);
        }
        for (int i = 500; i < webcamCursor.getCount(); i++) {
            webcamCursor.moveToPosition(i);
            float lat = Float.valueOf(webcamCursor.getString(webcamCursor
                    .getColumnIndex("Lat")));
            float lon = Float.valueOf(webcamCursor.getString(webcamCursor
                    .getColumnIndex("Lon")));
            GeoPoint gp = new GeoPoint((int) (lat / 10), (int) (lon / 10));
            myOverlay6.addPoint(gp);
        }
        System.out.println("**TIME**" + (System.currentTimeMillis() - t));

        mMap.getOverlays().add(myOverlay1);
        mMap.getOverlays().add(myOverlay2);
        mMap.getOverlays().add(myOverlay3);
        mMap.getOverlays().add(myOverlay4);
        mMap.getOverlays().add(myOverlay5);
        mMap.getOverlays().add(myOverlay6);

In my case, I'll override Overlay#draw() or MapView#draw() to check whether the points fall on the visible map area. Checking is better with sorted initial points array. But in first coding iteration I prefer to use the solution above (Profete 162).


Also something that might be very interesting.. http://code.google.com/p/mapview-overlay-manager/wiki/LazyLoading

I don't even tried it, but just suggesting some new ideas.

BTW I don't know if I should edit my previous answer rather that posting a new one, as this is totally different.

链接地址: http://www.djcxy.com/p/53722.html

上一篇: 如何检查在Android的根访问?

下一篇: (MapView和1700覆盖项目).equals(“Slow”)