问题
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.
I have used two major class from the library
1.BubbleDrawable 2.IconGenerator, I have used this two classes to create bitmap of my layout.
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.
回答1:
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.
来源:https://stackoverflow.com/questions/35378354/android-out-memory-exception-in-map