bigfish24/ABFRealmMapView

Add a tutorial to get items

Closed this issue · 3 comments

Hi Adam!
What do you think about to add a tutorial to get the itens inside de clustered item? I need do it on my application, but I don`t know how to do it :(

@ppamorim Hey, you can take items inside cluster on delegate methods of map view like this: didSelectAnnotation, after that, you can take ABFClusterAnnotation and inside annotation will be array with SafeRealmObjects

@ppamorim I added some helper methods to make it easier to get your objects. Check out this release: 55ade66

The example apps for Swift and Objective-C demonstrate how you can use the class method safeObjectsForClusterAnnotationView: on ABFClusterAnnotationView to retrieve the safe objects in the cluster from the annotation return in the MKMapViewDelegate mapView:didSelectAnnotationView:

Objective-C

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    NSArray<ABFLocationSafeRealmObject *> *safeObjects = [ABFClusterAnnotationView safeObjectsForClusterAnnotationView:view];

    ABFRestaurantObject *firstObject = safeObjects.firstObject.RLMObject;

    NSLog(@"First Object: %@",firstObject.name);
    NSLog(@"Cluster Count: %lu",safeObjects.count);
}

Swift

func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
        if let safeObjects = ABFClusterAnnotationView.safeObjectsForClusterAnnotationView(view) {

            if let firstObject = safeObjects.first?.toObject(ABFRestaurantObject) {
                print("First Object: \(firstObject.name)")
            }

            print("Count: \(safeObjects.count)")
        }
    }

@bigfish24 I noticed that RealmMapView-Swift, at line 92, needs to change this line to work:

For this:

#import <RealmMapView/RealmMapView.h>

To this:

#import <RealmMapView/RealmMapView-Swift.h>

Update:

The method toObject not exist. Is it correct? I`m using Swift 2.