我想用Google API显示许多图标(超过2,000个图标)。我基于此tuto创建了一个简单的活动。

访问地图时,它显示没有任何问题图标。但是,当我放大/缩小几次时,出现java.lang.OutOfMemoryError:

12-16 08:48:29.415: E/dalvikvm-heap(606): 680640-byte external allocation too large for this process.
12-16 08:48:29.415: E/GraphicsJNI(606): VM won't let us allocate 680640 bytes
12-16 08:48:29.415: D/AndroidRuntime(606): Shutting down VM
12-16 08:48:29.415: W/dalvikvm(606): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
12-16 08:48:29.425: E/AndroidRuntime(606): FATAL EXCEPTION: main
12-16 08:48:29.425: E/AndroidRuntime(606): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
12-16 08:48:29.425: E/AndroidRuntime(606):  at android.graphics.Bitmap.nativeCreate(Native Method)
12-16 08:48:29.425: E/AndroidRuntime(606):  at android.graphics.Bitmap.createBitmap(Bitmap.java:468)
12-16 08:48:29.425: E/AndroidRuntime(606):  at com.google.android.maps.ZoomHelper.createSnapshot(ZoomHelper.java:444)
12-16 08:48:29.425: E/AndroidRuntime(606):  at com.google.android.maps.ZoomHelper.doZoom(ZoomHelper.java:151)
12-16 08:48:29.425: E/AndroidRuntime(606):  at com.google.android.maps.ZoomHelper.doZoom(ZoomHelper.java:140)
12-16 08:48:29.425: E/AndroidRuntime(606):  at com.google.android.maps.MapView.doZoom(MapView.java:1478)
12-16 08:48:29.425: E/AndroidRuntime(606):  at com.google.android.maps.MapView.doZoom(MapView.java:1487)
12-16 08:48:29.425: E/AndroidRuntime(606):  at com.google.android.maps.MapView$4.onClick(MapView.java:1381)
12-16 08:48:29.425: E/AndroidRuntime(606):  at android.view.View.performClick(View.java:2408)
12-16 08:48:29.425: E/AndroidRuntime(606):  at android.view.View$PerformClick.run(View.java:8816)
12-16 08:48:29.425: E/AndroidRuntime(606):  at android.os.Handler.handleCallback(Handler.java:587)
12-16 08:48:29.425: E/AndroidRuntime(606):  at android.os.Handler.dispatchMessage(Handler.java:92)
12-16 08:48:29.425: E/AndroidRuntime(606):  at android.os.Looper.loop(Looper.java:123)
12-16 08:48:29.425: E/AndroidRuntime(606):  at android.app.ActivityThread.main(ActivityThread.java:4627)
12-16 08:48:29.425: E/AndroidRuntime(606):  at java.lang.reflect.Method.invokeNative(Native Method)
12-16 08:48:29.425: E/AndroidRuntime(606):  at java.lang.reflect.Method.invoke(Method.java:521)
12-16 08:48:29.425: E/AndroidRuntime(606):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-16 08:48:29.425: E/AndroidRuntime(606):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-16 08:48:29.425: E/AndroidRuntime(606):  at dalvik.system.NativeStart.main(Native Method)


这是我的活动方法:

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.google_map);

    Bundle bundle = getIntent().getExtras();

    //MAP
    mapView = (MapView) findViewById(R.id.mapView);

    //zoom設定
    LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);
    View zoomView = mapView.getZoomControls();

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

    //controller
    mc = mapView.getController();

    double[] adresse = (double[]) bundle.getDoubleArray(Site.LATTITUDE);
    p = new GeoPoint((int) (adresse[0] * 1E6), (int) (adresse[1] * 1E6));

    mc.animateTo(p);
    mc.setZoom(18);
    mapView.invalidate();

    List<Overlay> listOfOverlays = mapView.getOverlays();
    listOfOverlays.clear();

    try {

        //get datas from previous activiity
        List<SiteGoogle> sitesList = bundle.getParcelableArrayList(Site.JSON_ARRAY);

        //display message
        Toast.makeText(getBaseContext(), getResources().getString(R.string.map_toad), Toast.LENGTH_LONG).show();

        List<Overlay> mapOverlays = mapView.getOverlays();
        mapOverlays.clear();

        for(SiteGoogle site: sitesList) {

            //set icon
            Drawable drawable = this.getResources().getDrawable(R.drawable.icon01);

            //icon setting
            MapItemizedOverlay itemizedoverlay = new MapItemizedOverlay(drawable,this);

            //geoPoint
            GeoPoint point = new GeoPoint((int) (site.getLatitude() * 1E6), (int) (site.getLongitude() * 1E6));

            //item
            OverlayItem overlayitem = new OverlayItem(point, String.valueOf(site.getId()), null);

            itemizedoverlay.addOverlay(overlayitem);

            //add to map
            mapOverlays.add(itemizedoverlay);

        }

    } catch(Exception e) {
        Log.e(TAG,e.getMessage());
    }


还有我的MapItemizedOverlay类:

@SuppressWarnings("rawtypes")
public class MapItemizedOverlay extends ItemizedOverlay {

    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
    private Context mContext;

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

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

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

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

    @Override
    protected boolean onTap(int index){

        OverlayItem item = mOverlays.get(index);

        Intent intent = new Intent(mContext, SiteDetailsActivity.class);
        intent.putExtra(Site.ID, Integer.parseInt(item.getTitle()));
        mContext.startActivity(intent);

        return true;
    }


我不知道该怎么做才能使其更快而不崩溃。我应该使用画布还是缓存?我没主意

最佳答案

我不确定是否会有所帮助,但是您正在通过Overlay循环到地图中为每个site添加一个sitesList

for(SiteGoogle site: sitesList){

        //set icon
        Drawable drawable = this.getResources().getDrawable(R.drawable.icon01);


       //icon setting
       MapItemizedOverlay itemizedoverlay = new MapItemizedOverlay(drawable,this);

       //geoPoint
       GeoPoint point = new GeoPoint((int) (site.getLatitude() * 1E6), (int) (site.getLongitude() * 1E6));

       //item
       OverlayItem overlayitem = new OverlayItem(point, String.valueOf(site.getId()), null);

       itemizedoverlay.addOverlay(overlayitem);

       //add to map
       mapOverlays.add(itemizedoverlay);

        }


因此,您最终拥有2000个叠加层和2000个Drawables,这不好。

ItemizedOverlay可以以更有效的方式使用:

Drawable drawable = this.getResources().getDrawable(R.drawable.icon01);
MapItemizedOverlay itemizedoverlay = new MapItemizedOverlay(drawable,this);

for(SiteGoogle site: sitesList) {
    GeoPoint point = new GeoPoint((int) (site.getLatitude() * 1E6), (int) (site.getLongitude() * 1E6));
    OverlayItem overlayitem = new OverlayItem(point, String.valueOf(site.getId()), null);

    itemizedoverlay.addOverlay(overlayitem);
}
mapOverlays.add(itemizedoverlay);

09-20 14:06