gpt4 book ai didi

安卓 : Google Maps crashes when zooming (more than 1500 icons)

转载 作者:行者123 更新时间:2023-11-29 14:03:06 24 4
gpt4 key购买 nike

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

它在访问 map 时显示没有任何问题的图标。但是,当我放大/缩小几次时,会出现 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)

这是我的 Activity 方法:

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

我不知道我能做些什么来让它更快而不崩溃。我应该使用 Canvas 还是缓存?我没有想法。

最佳答案

我不确定它是否有帮助,但您在 sitesList 循环中为每个 site 添加了一个 Overlay 到 map :

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 个可绘制对象,这并不好。

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

关于安卓 : Google Maps crashes when zooming (more than 1500 icons),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8532266/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com