Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not working with dynamic loading #2

Closed
GoogleCodeExporter opened this issue Jul 24, 2015 · 3 comments
Closed

Not working with dynamic loading #2

GoogleCodeExporter opened this issue Jul 24, 2015 · 3 comments

Comments

@GoogleCodeExporter
Copy link

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
Copy link
Author

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

  • Changed state: NeedsMoreInfo

@GoogleCodeExporter
Copy link
Author

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
Copy link
Author

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

  • Changed state: Duplicate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant