I add Markers to a googleMap from an ArrayList of Objects. There are about 250 markers; I even have to convert them to bitmaps to customize them.
Just wanted to share a simple solution that I came up with for showing all markers without reloading every time.
Basically I rely on onCameraChangeListener to check if the user's view of the map has changed, and only show the markers within that view, otherwise the markers will be hidden... You will still have the longer load time the first time you load your data, but it's pretty quick after that.
First you need to add all the markers to your map, and store all the markers in a Map
// Put it in onCreate or something
map.clear(); // Clears markers
markers.clear(); // private Map markers = new HashMap()
for (Item item : items) {
MarkerOptions markerOptions = new MarkerOptions()
.position(new LatLng(item.getLatitude(), item.getLongitude())
.title(item.getTitle())
.snippet(item.getSnippet());
Marker marker = map.addMarker(marketOptions);
markers.put(marker, item);
}
Then you need to setup the listener:
// Put in onCreate or something
map.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
@Override
public void onCameraChange(final CameraPosition cameraPosition) {
showMarkers(cameraPosition.target);
LatLngBounds bounds = map.getProjection().getVisibleRegion().latLngBounds;
for (Map.Entry entry : markers) {
Marker marker = entry.getKey();
if (bounds.contains(marker.getPosition()) {
marker.setVisible(true);
} else {
marker.setVisible(false);
}
}
}
});
This should trigger at the end of animationMap and any form of camera movement (programatically, or by user dragging)
You may consider persisting the data using onSaveInstanceState to make it even more responsive when switching between apps