patrikbelis
patrikbelis

Reputation: 1360

More than one Pin annotation

I have annotation that will show on long press (1sec) and I want to create more than one annotation because now I can create only one annotation. So I want to ask you guys how can I do it?

Code for annotation setup:

in viewDidLoad
        var longPress = UILongPressGestureRecognizer(target: self, action: "addAnnotation:")
        longPress.minimumPressDuration = 1.0
        Mapa.addGestureRecognizer(longPress)

then I used this:

    func mapView(mapView: MKMapView!, didAddAnnotationViews views: [AnyObject]!) {
    if let tlacidlo = mapView.viewForAnnotation(mapView.userLocation) {
        tlacidlo.rightCalloutAccessoryView = UIButton.buttonWithType(.DetailDisclosure) as! UIButton
    }

}
func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
    if control == view.rightCalloutAccessoryView {
        self.performSegueWithIdentifier("segue", sender: self)
    } else {
        if let annotation = view.annotation as? MKPointAnnotation {
            self.Mapa.removeAnnotation(annotation)
        }
    }




}



func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {

    if annotation is MKUserLocation {
        //return nil
        return nil
    }

    let reuseId = "pin"
    var pinView = Mapa.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView

    if pinView == nil {
        //println("Pinview was nil")
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView!.canShowCallout = true
        pinView!.animatesDrop = true


        let deleteButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
        deleteButton.frame.size.width = 44
        deleteButton.frame.size.height = 45
        deleteButton.backgroundColor = UIColor.redColor()
        deleteButton.setImage(UIImage(named: "trash"), forState: .Normal)
        pinView!.leftCalloutAccessoryView = deleteButton

    }

    var button = UIButton.buttonWithType(UIButtonType.DetailDisclosure) as! UIButton

    pinView?.rightCalloutAccessoryView = button


    return pinView
}

func addAnnotation(gesture: UIGestureRecognizer) {

    if gesture.state == UIGestureRecognizerState.Began {

        var touch = gesture.locationInView(self.Mapa)
        var coordinate = Mapa.convertPoint(touch, toCoordinateFromView: self.Mapa)

        var location = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude)

        var loc = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)

        CLGeocoder().reverseGeocodeLocation(loc, completionHandler: { (placemarks, error) -> Void in

            if error == nil {
                let placemark = CLPlacemark(placemark: placemarks[0] as! CLPlacemark)

                self.cislo = placemark.subThoroughfare != nil ? placemark.subThoroughfare : ""
                self.adresa = placemark.thoroughfare != nil ? placemark.thoroughfare : ""
                self.mesto = placemark.subAdministrativeArea != nil ? placemark.subAdministrativeArea : ""
                self.krajina = placemark.administrativeArea != nil ? placemark.administrativeArea : ""

                self.annotation.coordinate = location
                self.annotation.title = self.adresa! + " " + self.cislo!
                self.Mapa.addAnnotation(self.annotation)
                println("Špendlík pridaný!")


            }

        })

    }

}

Upvotes: 0

Views: 659

Answers (1)

zisoft
zisoft

Reputation: 23078

I can see nothing wrong with your code. The only thing that irritates me is this line:

self.annotation.coordinate = location

It looks like self.annotation is an instance variable which you reuse for any new annotation. Here is a code snippet from one of my apps where multiple annotations work without problems:

@IBAction func setPin(sender: UILongPressGestureRecognizer) {
    if sender.state == UIGestureRecognizerState.Began {
        let viewLocation = sender.locationInView(self.mapView)
        let location = self.mapView.convertPoint(viewLocation, toCoordinateFromView: self.mapView)

        let pinAnnotation = PinAnnotation()  // PinAnnotation is my custom annotation class
        pinAnnotation.setCoordinate(location)

        self.mapView.addAnnotation(pinAnnotation)
    }
}

Upvotes: 2

Related Questions