Android out memory exception in map

后端 未结 1 662
一个人的身影
一个人的身影 2021-01-28 00:01

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

相关标签:
1条回答
  • 2021-01-28 00:37

    From the comments:

    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.

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

    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.

    0 讨论(0)
提交回复
热议问题