amirpiri / android-maps-extensions

Automatically exported from code.google.com/p/android-maps-extensions
0 stars 0 forks source link

Not working with dynamic loading #2

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
I've a lot of poi in the map(22k), so I dynamically add and remove markers as 
the maps is scrolled or zoomed in/out.

In this case clustering isn't working, because in 
DelegatingOnCameraChangeListener.onCameraChange
clusteringStrategy.onZoomChange is called - and new cluster is calculated
then
onCameraChangeListener.onCameraChange is called - and new data is inserted but 
clusters are not updated.

These functions should be called in reverse order, so that 
clusteringStrategy.onZoomChange is called with the new data and cluster could 
be correctly computed

Original issue reported on code.google.com by verna...@gmail.com on 20 Mar 2013 at 5:17

GoogleCodeExporter commented 8 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

GoogleCodeExporter commented 8 years ago
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

GoogleCodeExporter commented 8 years ago

Original comment by maciek.g...@gmail.com on 24 Mar 2013 at 6:30