2

我在 Android 的 Google Map v2 上加载了很多标记(1700 或更多)。我想减少创建此地图所使用的内存量,因为它似乎是我的 2.3 Droid 的 FC。我只想在可见屏幕区域内添加点。

我查看了 Google Maps Extension 并看到了这个:

map.setClustering(new ClusteringSettings().enabled(false).addMarkersDynamically(true));

但是我的 for 循环将所有标记添加到地图中。我如何设置它并仅按此方法添加我的标记?

这是我的 for 循环添加标记:

int nsize = visibleMarkers.size();
                for (int i = 0; i < nsize; i++) {
                    MapMarkers marks = new MapMarkers();
                    String title = visibleMarkers.valueAt(i).getTitle();
                    String desc = visibleMarkers.valueAt(i).getDesc();
                    Float latitude = visibleMarkers.valueAt(i).getLat();
                    Float longitude = visibleMarkers.valueAt(i).getLon();

                    m = map.addMarker(new MarkerOptions()
                            .position(new LatLng(latitude, longitude))
                            .title(title)
                            .icon(BitmapDescriptorFactory
                                    .fromResource(R.drawable.snotel_marker)));

                    marks.setTitle(title);
                    marks.setDesc(desc);

                    markerInfo.put(m, marks);

                    map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
                        @Override
                        public void onInfoWindowClick(Marker marker) {

                            MapMarkers markInfo = markerInfo.get(marker);

                            Intent i = new Intent(MainActivity.this,
                                    MarkerInformation.class);
                            i.putExtra("name", markInfo.getTitle()).putExtra(
                                    "description", markInfo.getDesc());
                            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(i);

                        }

                    });
                }

如何仅在显示屏幕区域时添加它们。这会减少我设备上的资源使用吗?

编辑::: 这是 logcat 问题

10-01 10:59:41.590: D/dalvikvm(6488): GC_FOR_MALLOC freed <1K, 55% free 10009K/21895K, external 2433K/2688K, paused 94ms
10-01 10:59:41.691: D/dalvikvm(6488): GC_FOR_MALLOC freed 128K, 55% free 9913K/21895K, external 2433K/2688K, paused 72ms
10-01 10:59:41.793: D/dalvikvm(6488): GC_FOR_MALLOC freed 62K, 55% free 9973K/21895K, external 2433K/2688K, paused 83ms
10-01 10:59:41.910: D/dalvikvm(6488): GC_FOR_MALLOC freed 65K, 55% free 10026K/21895K, external 2433K/2688K, paused 74ms
10-01 10:59:41.988: D/dalvikvm(6488): GC_FOR_MALLOC freed 0K, 54% free 10085K/21895K, external 2433K/2688K, paused 72ms
10-01 10:59:41.988: I/dalvikvm-heap(6488): Grow heap (frag case) to 14.703MB for 60434-byte allocation
10-01 10:59:42.082: D/dalvikvm(6488): GC_FOR_MALLOC freed 0K, 54% free 10144K/21959K, external 2433K/2688K, paused 82ms
10-01 10:59:42.199: D/dalvikvm(6488): GC_FOR_MALLOC freed 138K, 55% free 10043K/21959K, external 2433K/2688K, paused 82ms
10-01 10:59:42.207: I/dalvikvm-heap(6488): Grow heap (frag case) to 14.762MB for 164912-byte allocation
10-01 10:59:42.277: D/dalvikvm(6488): GC_FOR_MALLOC freed 8K, 54% free 10196K/22151K, external 2433K/2688K, paused 68ms
10-01 10:59:42.730: D/dalvikvm(6488): GC_FOR_MALLOC freed 509K, 55% free 10017K/22151K, external 2433K/2688K, paused 72ms
10-01 10:59:42.879: D/dalvikvm(6488): GC_FOR_MALLOC freed 364K, 55% free 10066K/22151K, external 2433K/2688K, paused 78ms
10-01 10:59:42.988: D/dalvikvm(6488): GC_FOR_MALLOC freed 39K, 55% free 10117K/22151K, external 2433K/2688K, paused 82ms
10-01 10:59:42.988: I/dalvikvm-heap(6488): Forcing collection of SoftReferences for 201280-byte allocation
10-01 10:59:43.082: D/dalvikvm(6488): GC_FOR_MALLOC freed 8K, 55% free 10108K/22151K, external 2433K/2688K, paused 86ms
10-01 10:59:43.082: E/dalvikvm-heap(6488): Out of memory on a 201280-byte allocation.
10-01 10:59:43.082: I/dalvikvm(6488): "vts_com.jasonflaherty.snoteldata" prio=5 tid=10 RUNNABLE
10-01 10:59:43.082: I/dalvikvm(6488):   | group="main" sCount=0 dsCount=0 obj=0x405c8fe0 self=0x1c1ec0
10-01 10:59:43.082: I/dalvikvm(6488):   | sysTid=6533 nice=10 sched=0/0 cgrp=bg_non_interactive handle=1628832
10-01 10:59:43.082: I/dalvikvm(6488):   at maps.ao.d.a((null):~-1)
10-01 10:59:43.082: I/dalvikvm(6488):   at maps.i.cu.a((null):-1)
10-01 10:59:43.082: I/dalvikvm(6488):   at maps.k.ah.a((null):-1)
10-01 10:59:43.082: I/dalvikvm(6488):   at maps.k.d.a((null):-1)
10-01 10:59:43.082: I/dalvikvm(6488):   at maps.k.g.handleMessage((null):-1)
10-01 10:59:43.082: I/dalvikvm(6488):   at android.os.Handler.dispatchMessage(Handler.java:99)
10-01 10:59:43.082: I/dalvikvm(6488):   at android.os.Looper.loop(Looper.java:130)
10-01 10:59:43.082: I/dalvikvm(6488):   at maps.k.d.c((null):-1)
10-01 10:59:43.082: I/dalvikvm(6488):   at maps.k.ag.c((null):-1)
10-01 10:59:43.082: I/dalvikvm(6488):   at maps.ao.b.run((null):-1)
10-01 10:59:43.129: W/dalvikvm(6488): threadid=10: thread exiting with uncaught exception (group=0x40015560)
10-01 10:59:43.144: E/AndroidRuntime(6488): FATAL EXCEPTION: vts_com.jasonflaherty.snoteldata
10-01 10:59:43.144: E/AndroidRuntime(6488): java.lang.OutOfMemoryError
10-01 10:59:43.144: E/AndroidRuntime(6488):     at maps.ao.d.a(Unknown Source)
10-01 10:59:43.144: E/AndroidRuntime(6488):     at maps.i.cu.a(Unknown Source)
10-01 10:59:43.144: E/AndroidRuntime(6488):     at maps.k.ah.a(Unknown Source)
10-01 10:59:43.144: E/AndroidRuntime(6488):     at maps.k.d.a(Unknown Source)
10-01 10:59:43.144: E/AndroidRuntime(6488):     at maps.k.g.handleMessage(Unknown Source)
10-01 10:59:43.144: E/AndroidRuntime(6488):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-01 10:59:43.144: E/AndroidRuntime(6488):     at android.os.Looper.loop(Looper.java:130)
10-01 10:59:43.144: E/AndroidRuntime(6488):     at maps.k.d.c(Unknown Source)
10-01 10:59:43.144: E/AndroidRuntime(6488):     at maps.k.ag.c(Unknown Source)
10-01 10:59:43.144: E/AndroidRuntime(6488):     at maps.ao.b.run(Unknown Source)
10-01 10:59:43.394: D/dalvikvm(6488): GC_FOR_MALLOC freed 195K, 55% free 10116K/22151K, external 2433K/2688K, paused 153ms
10-01 10:59:43.433: W/ResourceType(6488): getEntry failing because entryIndex 939 is beyond type entryCount 1
10-01 10:59:43.433: W/ResourceType(6488): Failure getting entry for 0x7f0b03ab (t=10 e=939) in package 0 (error -2147483647)
10-01 10:59:43.433: E/GooglePlayServicesUtil(6488): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
10-01 10:59:43.769: D/dalvikvm(6488): GC_EXTERNAL_ALLOC freed 115K, 55% free 10102K/22151K, external 2433K/2688K, paused 121ms
10-01 10:59:43.824: E/dalvikvm-heap(6488): 80-byte external allocation too large for this process.
10-01 10:59:43.824: W/OSMemory(6488): External allocation of 80 bytes was rejected
10-01 10:59:43.925: D/dalvikvm(6488): GC_FOR_MALLOC freed <1K, 55% free 10102K/22151K, external 2433K/2688K, paused 101ms
10-01 10:59:44.019: W/dalvikvm(6488): threadid=17: thread exiting with uncaught exception (group=0x40015560)
10-01 10:59:44.027: I/Process(6488): Sending signal. PID: 6488 SIG: 9
4

1 回答 1

2

我如何设置它并仅按此方法添加我的标记?

谷歌地图扩展

map.setClustering(new ClusteringSettings().enabled(false).addMarkersDynamically(true));

方法正是这样做的。仅将Marker可见区域中的 s 添加到地图。它之外的所有标记都由 Extensions lib 保留,但不会进行进程间通信以将它们放在地图上。当用户滚动显示不同的区域时,它们将被添加。

如果您认为地图有问题,我会尝试的第一件事是使用默认标记图标或只BitmapDescriptor为所有Markers 创建一个:

BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.snotel_marker));
for (int i = 0; i < nsize; i++) {
    // ...
    m = map.addMarker(new MarkerOptions()
                        .position(new LatLng(latitude, longitude))
                        .title(title)
                        .icon(icon);
    // ...
}

这应该可以解决OutOfMemoryError问题。


除此之外,您不想setOnInfoWindowClickListener循环调用。一次就足够了。
还有一件事:如果这是您想要保留的所有数据,您可以使用MarkerOptions.snippetMarker.getSnippet为您的String desc. 无需将其全部保存在Map<Marker, MapMarkers>数据结构中。String如果您想在两个s 和旁边保留其他数据LatLng,我建议使用MarkerOptions.data(marks)(从 1.6 开始)或Marker.setData(marks)在 Extensions lib 中。稍后在OnInfoWindowClickListener回调中阅读它:

MapMarkers marks = (MapMarkers) marker.getData();
于 2013-10-01T17:11:19.703 回答