Closed GoogleCodeExporter closed 9 years ago
Tested with code below and it works correctly.
Can you please provide code you are using?
If you are using map.clear(), then this is related to Issue 3. Also be aware
using clear might not be optimal, as most of the markers will be added again.
Because what you are doing is a very common code, this will also be added to
the library. See Issue 1.
// code inside onCreate
Random r = new Random();
final List<LatLng> toBeAdded = new ArrayList<LatLng>();
for (int i = 0; i < 20000; i++) {
LatLng position = new LatLng(r.nextDouble() * 60 - 30, r.nextDouble() * 100 - 50);
toBeAdded.add(position);
}
final List<Marker> addedMarkers = new ArrayList<Marker>();
map.setOnCameraChangeListener(new OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition cameraPosition) {
Projection projection = map.getProjection();
LatLngBounds bounds = projection.getVisibleRegion().latLngBounds;
// remove markers outside of VisibleRegion:
Iterator<Marker> addedMarkersIterator = addedMarkers.iterator();
while (addedMarkersIterator.hasNext()) {
Marker marker = addedMarkersIterator.next();
LatLng position = marker.getPosition();
if (!bounds.contains(position)) {
marker.remove();
toBeAdded.add(position);
addedMarkersIterator.remove();
}
}
// add markers inside VisibleRegion:
BitmapDescriptor icon = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA);
MarkerOptions options = new MarkerOptions().icon(icon);
Iterator<LatLng> toBeAddedIterator = toBeAdded.iterator();
while (toBeAddedIterator.hasNext()) {
LatLng position = toBeAddedIterator.next();
if (bounds.contains(position)) {
Marker marker = map.addMarker(options.position(position));
addedMarkers.add(marker);
toBeAddedIterator.remove();
}
}
}
});
Original comment by maciek.g...@gmail.com
on 22 Mar 2013 at 2:17
Thanks, I confirm that the problem is related to map.clear() and your solution
in issue 3 fixed the problem
Unfortunately I can't completely avoid map.clear() as I need to update marker
icons too, and currently the only way to obtain that is to remove the marker
and inserting a new one
Original comment by verna...@gmail.com
on 22 Mar 2013 at 10:59
Original comment by maciek.g...@gmail.com
on 24 Mar 2013 at 6:30
Original issue reported on code.google.com by
verna...@gmail.com
on 20 Mar 2013 at 5:17