Giter Site home page Giter Site logo

Comments (5)

chenr2 avatar chenr2 commented on September 28, 2024

What is the reason behind using UIGestureRecognizer? And what is the relation between animated and viewDidLoad()? And can you send me a small sample project that demonstrates the bug you're experiencing?

from fbannotationclusteringswift.

mamoun1101 avatar mamoun1101 commented on September 28, 2024

You can see below you project with this class FBViewController: UIViewController with one pices of code I added to drop a pin with UIGesturRecognizer :
Just replace your class FBViewController with this code below and you will notice when you drop a pin, it will disappear once you go left or right or zoom out or in in the map.
Thank you

class FBViewController: UIViewController {

@IBOutlet weak var mapView: MKMapView!

let numberOfLocations = 1000

let clusteringManager = FBClusteringManager()



override func viewDidLoad() {
    super.viewDidLoad()

    let array:[MKAnnotation] = randomLocationsWithCount(numberOfLocations)

    clusteringManager.addAnnotations(array)
    clusteringManager.delegate = self;

    mapView.centerCoordinate = CLLocationCoordinate2DMake(0, 0);
    let uilpgr = UILongPressGestureRecognizer(target: self, action: "action:")
    uilpgr.minimumPressDuration = 0.1
    mapView.addGestureRecognizer(uilpgr)

}

func action(longPressGestureRecognizer:UILongPressGestureRecognizer){
    if longPressGestureRecognizer.state == UIGestureRecognizerState.Began {
        let touchPoint = longPressGestureRecognizer.locationInView(self.mapView)
        let newCoordinate = self.mapView.convertPoint(touchPoint, toCoordinateFromView: self.mapView)
        // to know where exactly the pin is (adress)
        let location = CLLocation(latitude : newCoordinate.latitude, longitude:  newCoordinate.longitude)
                     let annotation = MKPointAnnotation()
           annotation.coordinate = newCoordinate
            annotation.title = title
            print("\(title)")
            self.mapView.addAnnotation(annotation)
        }
    }
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Utility

func randomLocationsWithCount(count:Int) -> [FBAnnotation] {
    var array:[FBAnnotation] = []
    for _ in 0...count {
        let a:FBAnnotation = FBAnnotation()
        a.coordinate = CLLocationCoordinate2D(latitude: drand48() * 40 - 20, longitude: drand48() * 80 - 40 )
        array.append(a)
        print("\(array)")
    }
    return array
}

}

from fbannotationclusteringswift.

chenr2 avatar chenr2 commented on September 28, 2024

When you pan the map, regionDidChangeAnimated gets called. Within this delegate method, the clusteringManager will displayAnnotations that intersect with the current map region (clusteredAnnotationsWithinMapRect). In other words, panning the map refreshes it with all the annotations already loaded into the clustering manager.

You probably want to add the annotation to the clustering manager (in addition to dropping it on the map). Within your action method, try adding this line to the end:

clusteringManager.addAnnotations([annotation])

from fbannotationclusteringswift.

mamoun1101 avatar mamoun1101 commented on September 28, 2024

Awesome thank you ! I have another question, How to retrieve the title and the subtitle of the cluster inside the viewcontroller ?

from fbannotationclusteringswift.

chenr2 avatar chenr2 commented on September 28, 2024

I think it might make more sense to get the title and subtitle of the pin rather than the cluster. This is how you could go about it:

In FBAnnotation.swift, add a subtitle property:

class FBAnnotation : NSObject {
    var coordinate = CLLocationCoordinate2D(latitude: 39.208407, longitude: -76.799555)
    var title: String? = ""
    var subtitle: String? = "" // NEW
}

In ViewController.swift, populate the title and subtitle when you create the random pins:

    func randomLocationsWithCount(count:Int) -> [FBAnnotation] {
        var array:[FBAnnotation] = []
        for _ in 0...count {
            let a:FBAnnotation = FBAnnotation()
            a.coordinate = CLLocationCoordinate2D(latitude: drand48() * 40 - 20, longitude: drand48() * 80 - 40 )
            a.title = "lat: \(a.coordinate.latitude)"  // NEW
            a.subtitle = "long: \(a.coordinate.longitude)" // NEW
            array.append(a)
        }
        return array
    }

Enable the callout when a pin is tapped:

    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
        var reuseId = ""
        if annotation.isKindOfClass(FBAnnotationCluster) {
            reuseId = "Cluster"
            var clusterView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
            clusterView = FBAnnotationClusterView(annotation: annotation, reuseIdentifier: reuseId)
            return clusterView
        } else {
            reuseId = "Pin"
            var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            pinView!.pinTintColor = UIColor.greenColor()
            pinView?.canShowCallout = true   // NEW!
            return pinView
        }
    }

Since the gesture recognizer conflicts with the pin callout, comment out the following line:

//        mapView.addGestureRecognizer(uilpgr)

from fbannotationclusteringswift.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.