多个Pin注释

时间:2015-05-19 16:51:27

标签: ios xcode swift annotations mapkit

我有一个注释,将在长按(1秒)显示,我想创建多个注释,因为现在我只能创建一个注释。所以我想问你们,我该怎么做?

注释设置代码:

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

然后我用了这个:

    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ý!")


            }

        })

    }

}

1 个答案:

答案 0 :(得分:2)

我看到您的代码没有任何问题。唯一令我恼火的是这条线:

self.annotation.coordinate = location

看起来self.annotation是一个实例变量,您可以将其重用于任何新注释。以下是我的某个应用中的代码段,其中多个注释可以正常运行:

@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)
    }
}
相关问题