(MapView和1700 Overlays Items).equals(“Slow”)

时间:2011-07-19 19:28:18

标签: java android google-maps

我有MapView并且我在其上覆盖了1,700个点,每个点都有相同的可绘制但信息不同。我目前正在使用Itemized Overlay添加所有叠加层,然后填充一次。这样可行,但性能很慢。改变缩放级别和焦点是跳跃的。现在,使用ArrayItemizedOverlay会更好吗,因为它是相同的可绘制的,或者地图是否同样慢?

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

我如何添加项目:

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

3 个答案:

答案 0 :(得分:4)

这不是解决方案,而是在等待完美解决方案时的解决方法。

我遇到了同样的问题并创建了多个叠加层。

带有99个点的6个叠加层实际上比带有600个点的1个叠加层更快。

(15秒加载时间Vs 1秒)

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

答案 1 :(得分:0)

在我的情况下,我会覆盖Overlay#draw()MapView#draw()来检查点是否落在可见地图区域。排序的初始点数组检查更好。 但在第一次编码迭代中,我更喜欢使用上面的解决方案(Profete 162)。

答案 2 :(得分:0)

也可能非常有趣.. http://code.google.com/p/mapview-overlay-manager/wiki/LazyLoading

我甚至没有尝试过,只是提出了一些新的想法。

BTW我不知道我是否应该编辑我以前的答案,而不是发布一个新答案,因为这完全不同。