【发布时间】:2015-05-19 16:51:27
【问题描述】:
我的注释将在长按(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ý!")
}
})
}
}
【问题讨论】:
-
添加部分可以放在for循环中,但是所有注解的坐标呢?
-
请更新您的问题并显示
self.annotion是什么 -
每个注释必须是一个单独的实例。
self.annotation是单个实例。addAnnotation忽略添加已添加对象的调用,因此您的代码只是更改添加的第一个(也是唯一一个)注释的坐标和标题。见stackoverflow.com/questions/30215109/…。 for 循环在这里无关紧要。self.annotation不应该是实例变量。使其成为 reverseGeocodeLocation 完成处理程序中的局部变量并在那里创建一个实例。
标签: ios xcode swift annotations mapkit