出的Andr​​oid地图中的内存异常 [英] Android out memory exception in map

查看:296
本文介绍了出的Andr​​oid地图中的内存异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid应用程序,我称之为服务器请求每10秒和更新结果地图标记(创建位图,我的标记具有图像和放大器;文本)。我使用 Android的地图utils的库创建我的布局位图,这是工作正常,但我力一些时间与内存不足的问题后关闭。

In my Android application I call server request every 10 seconds and update result in map with marker (creating bitmap, my marker has image & text). I use Android map utils library to create bitmap of my layout, this is working fine but my force close after some time with out of memory issue.

Note:
My marker has image and text like a layout.
its updates with dynamic result by every 10 seconds.

我已经使用两大类从href=\"https://github.com/googlemaps/android-maps-utils\" rel=\"nofollow\">库的

1 <一href=\"https://github.com/googlemaps/android-maps-utils/blob/master/library/src/com/google/maps/android/ui/BubbleDrawable.java\"相对=nofollow> BubbleDrawable
2. <一个href=\"https://github.com/googlemaps/android-maps-utils/blob/master/library/src/com/google/maps/android/ui/IconGenerator.java\"相对=nofollow> IconGenerator ,我已经使用这个两个类创建我的布局位图。

1.BubbleDrawable 2.IconGenerator, I have used this two classes to create bitmap of my layout.

在我的地图我的活动调用库每10秒,并刷新地图自定义标记。

in my Map Activity I call the library every 10 seconds and refresh the map with custom markers.

code:

public class MainActivity extends Activity{

 private IconGenerator  icGen ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         icGen = new IconGenerator(this);
         serverResponse(); // this method call every 10 seconds
    } // onCreate ends here.

    void updateResult(String placeName){
         markerPin = googleMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(icGen.makeIcon(placeName))) // dynamic name 
             .position(markerLatLng).anchor(0.5f, 1.0f)); // I got out of memory exception here
        ....
        googleMap.addMarker(markerPin);     
    }

    void serverResponse(){
        JSONArray arr = new JSONArray(serverResponse);
        for(int i=0;i<all.length();i++){
          name = arr.getJSONObject.getStrig(name);
          updateResult(name);
        }
    }
}

我也没办法解决这个问题,请帮助我。

I have no idea to resolve this problem, please help me.

推荐答案

从注​​释:

我会说通过保持一个大数目,你的内存不足造成
  标记在地​​图中。你真的不需要维护所有标记
  在地图中(它会破坏性能),你只需要显示
  可见的。所以,你可以,例如删除所有标记
  (map.clear()),并添加在onCameraChange事件可见的。至
  做到这一点,你需要保持一个结构来存储位置
  您从您的服务下载和检查的地方都在
  可见口中。

I would say that your OutOfMemory is caused by keeping a big number of markers in your map. You really don't need to maintain all the markers in your map (it will undermine the performance), you only need to show the visible ones. So you can, for example remove all the markers (map.clear()) and add the visible ones on the onCameraChange event. To do this, you will need to maintain a structure to store the locations that you download from your service and check if the places are in the visible viewport.

下面是清除地图每次相机更改,并添加所有可见的标记的例子(我只显示相关的code,请考虑到它是没有测试。)

Here is an example that clears the map each time the camera changes and adds all the visible markers (I'm showing only the relevant code. Please take into account that it is no tested):

public class MainActivity extends Activity implements GoogleMap.OnCameraChangeListener, GoogleMap.OnMapReadyCallback {
    private GoogleMap googleMap;
    private List<Place> places = new ArrayList<>();

    // ...

    void serverResponse() {
        JSONArray arr = new JSONArray(serverResponse);
        for (int i = 0; i < all.length(); i++) {
            name = arr.getJSONObject.getStrig(name);

            // TODO: Get the LatLong

            // Add a new Place to the list 
            places.add(new Place(name, latLng));
        }
    }

    @Override
    public void onCameraChange(final CameraPosition cameraPosition) {
        // Clear the map to avoid keeping tons of markers
        googleMap.clear();

        // Find the current visible region
        final LatLngBounds bounds = googleMap.getProjection()
                .getVisibleRegion().latLngBounds;

        // Draw the visible markers
        for (Place place : places) {
            if (bounds.contains(place.getLatLng())) {
                googleMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(icGen.makeIcon(place.getName())))
                        .position(place.getLatLng()).anchor(0.5f, 1.0f));
            }
        }
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        this.googleMap = googleMap;
        googleMap.setOnCameraChangeListener(this);
    }

    private class Place {
        private String name;
        private LatLng latLng;

        public Place(String name, LatLng latLng) {
            this.name = name;
            this.latLng = latLng;
        }

        public String getName() {
            return name;
        }

        public void setName(final String name) {
            this.name = name;
        }

        public LatLng getLatLng() {
            return latLng;
        }

        public void setLatLng(final LatLng latLng) {
            this.latLng = latLng;
        }
    }
}

也许你需要提高code更改列表&LT;广场&GT; 是一个地图&LT;弦乐,地点和GT; ,以确保您不添加特定地方的两倍。

Maybe you need to improve the code changing the List<Place> to be a Map<String, Place> to ensure that you don't add a given place twice.

这篇关于出的Andr​​oid地图中的内存异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆