Android googlemap内存不足

时间:2010-05-17 02:38:30

标签: android google-maps google-maps-api-3 maps out-of-memory

我使用googlemap api创建了一个Android应用程序,并在叠加层上绘制了一些16x16 png(大约200个计数)。当我在mapview中移动或放大时,经常出现“内存不足”错误。

我还在我的htc中使用了googlemap应用程序。它使用大约14 + MB内存的接缝,并且永远不会发生“内存不足”。

如何在googlemap api中保存memmory用法,或者如何放大android api memmory limit。

非常感谢!

2 个答案:

答案 0 :(得分:2)

我自己的解决方案: 在zoomin / zoomout时捕获OutOfMemoryError,将阻止api被VM杀死。 因为它经常在翻译后进行地图缩放时死亡。

    mapView.setBuiltInZoomControls(true);  
    ZoomButtonsController zoomctrl = mapView.getZoomButtonsController(); 
    zoomctrl.setAutoDismissed(false);//自动隐藏关闭
    zoomctrl.setVisible(true);
    zoomctrl.setOnZoomListener(new ZoomButtonsController.OnZoomListener() {

        public void onZoom(boolean zoomIn) {
            // TODO Auto-generated method stub
            try{
                Log.i(TAG, "OnZoomListener");
                System.gc();
                if(zoomIn)
                {                       
                    mc.zoomIn();
                }
                else
                {
                    mc.zoomOut();
                }
                System.gc();
            }
            catch(OutOfMemoryError e)
            {
                e.printStackTrace();
                Log.e(TAG, e.toString());
                Toast.makeText(GoogleMap.this, e.toString(), Toast.LENGTH_LONG);
            }
            catch (Exception e)
            {
                Log.w(TAG, e.toString());
                Toast.makeText(GoogleMap.this, e.toString(), Toast.LENGTH_LONG);
            }               
        }

        public void onVisibilityChanged(boolean visible) {
            // TODO Auto-generated method stub

        }
    });

    private boolean myDoubleTouch(float x, float y, MapView mapView)
    {
    Log.i(mParent.TAG, "myDoubleTouch: " + x +","+y);
    try
    {
        mapView.getController().zoomInFixing((int)x, (int)y);
    }
    catch(OutOfMemoryError e)
    {
        System.gc();
        e.printStackTrace();
        Log.e(mParent.TAG, e.toString());
        Toast.makeText(m_mapview.getContext(), e.toString(), Toast.LENGTH_LONG);
    }
    catch (Exception e)
    {
        Log.w(mParent.TAG, e.toString());
        Toast.makeText(m_mapview.getContext(), e.toString(), Toast.LENGTH_LONG);
    }           

    return true;
}

答案 1 :(得分:0)

public class CustomMapView extends MapView {
    // private Context context;
    public CustomMapView(Context context, String apiKey) {

    super(context, apiKey);
    // this.context = context;
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    try {
        return super.dispatchTouchEvent(ev);

    } catch (Exception ex) {

            //     Log.e("CustomMapView", "Caught the exception");
        int zoomLevel = getZoomLevel();
        getController().setZoom(zoomLevel-1);
        super.setVisibility(View.GONE);
        super.setVisibility(View.VISIBLE);
    }

    return true;
}

@Override
public void draw(Canvas canvas) {
    try {
        super.draw(canvas);
    } catch (Exception e) {

             //     Log.e("CustomMapView", "Caught the exception");
        int zoomLevel = getZoomLevel();
        getController().setZoom(zoomLevel-1);
        super.setVisibility(View.GONE);
        super.setVisibility(View.VISIBLE);
    }
}

public CustomMapView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // this.context = context;
}

public CustomMapView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // this.context = context;
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    System.gc();
    return super.onTouchEvent(ev);
}

}

与onZoomListener一起,我添加了自己的CustomMapView,它扩展了MapView。这解决了我的应用程序中的内存异常。