Ios 多个接点注释
我有将在长按(1秒)时显示的注释,我想创建多个注释,因为现在我只能创建一个注释。所以我想问你们我怎么做 注释设置的代码:Ios 多个接点注释,ios,xcode,swift,annotations,mapkit,Ios,Xcode,Swift,Annotations,Mapkit,我有将在长按(1秒)时显示的注释,我想创建多个注释,因为现在我只能创建一个注释。所以我想问你们我怎么做 注释设置的代码: in viewDidLoad var longPress = UILongPressGestureRecognizer(target: self, action: "addAnnotation:") longPress.minimumPressDuration = 1.0 Mapa.addGestureRecognizer(lo
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ý!")
}
})
}
}
我看不出你的代码有什么问题。唯一让我恼火的是这句话:
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)
}
}
您可以将添加部分放入for循环,但所有注释的坐标如何?请更新您的问题并显示
self。注释i每个注释必须是单独的实例self.annotation
是一个实例addAnnotation
忽略添加已添加对象的调用,因此您的代码只是更改添加的第一个(也是唯一一个)注释的坐标和标题。看见for循环在这里是不相关的self.annotation
不应是实例变量。将其设置为reverseGeocodeLocation完成处理程序中的局部变量,并在那里创建一个实例。您能帮我解决这个问题吗??