如何使用 swift2 将不同的按钮操作添加到地图视图中的第二个注释 /pin



任何人都可以帮我向第二个注释/pin(annotation2)添加不同的按钮操作,现在按钮在两个注释引脚中做相同的工作如何相互做不同的工作。我在我的项目中使用 Swift3,这是我的代码。谢谢

这是我的代码。

  import UIKit
  import MapKit
  import CoreLocation
 class MyAnnotation: MKPointAnnotation {
      var uniqueId: Int!
 }
  class LocationViewController: UIViewController , MKMapViewDelegate , CLLocationManagerDelegate{
    @IBOutlet weak var map: MKMapView!{
    didSet{
        map.delegate = self
    }
}
@IBOutlet weak var locationInfo: UITextView!

override func viewDidLoad() {
    super.viewDidLoad()

    let locations = CLLocationCoordinate2DMake(33.314627, 44.303500)
    let location2 = CLLocationCoordinate2DMake(33.312149, 44.3024567)
    let span = MKCoordinateSpanMake(0.02, 0.02)
    let span2 = MKCoordinateSpanMake(0.02, 0.02)
    let region = MKCoordinateRegionMake(locations, span)
     let region2 = MKCoordinateRegionMake(location2, span2)

    map.setRegion(region, animated: true)
    map.setRegion(region2, animated: true)

    let annotation = MyAnnotation()
    //annotation.setCoordinate(location)
    annotation.coordinate = locations
    annotation.title = "Zaid Homes"
    annotation.subtitle = "Hay aljameaa"
    annotation.uniqueId = 1
    map.addAnnotation(annotation)
    let annotation2 = MyAnnotation()
    //annotation.setCoordinate(location)
    annotation2.coordinate = location2
    annotation2.title = "Zaid "
    annotation2.subtitle = "aljameaa"
    annotation.uniqueId = 2
    map.addAnnotation(annotation2)
    //Showing the device location on the map
    self.map.showsUserLocation = true;
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    var view = mapView.dequeueReusableAnnotationView(withIdentifier: "AnnotationView Id")
    if view == nil{
        view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "AnnotationView Id")
        view!.canShowCallout = true
    } else {
        view!.annotation = annotation
    }
    view?.leftCalloutAccessoryView = nil
    view?.rightCalloutAccessoryView = UIButton(type: UIButtonType.detailDisclosure)

    return view
}
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    if (control as? UIButton)?.buttonType ==   UIButtonType.detailDisclosure {
        mapView.deselectAnnotation(view.annotation, animated: false)
        if let myAnnotation = view.annotation as? MyAnnotation {
            if (myAnnotation.uniqueId == 1) {
                performSegue(withIdentifier: "info", sender: view)
            }
            else  {
                performSegue(withIdentifier: "info2", sender: view)
            }
        }            
    }
}
}

了解您点击哪个注释的最简单方法是创建自定义注释类并添加其注释。因此,创建一个注释类MyAnnotation MKPointAnnotation子类,并使用多个注释维护一个 uniqueId。

class MyAnnotation: MKPointAnnotation {
    var uniqueId: Int!
}

现在您需要添加类型为 MyAnnotation 而不是 MKPointAnnotation 的注释。

let annotation = MyAnnotation()
annotation.coordinate = locations
annotation.title = "Zaid Homes"
annotation.subtitle = "Hay aljameaa"
//Set uniqueId for annotation
annotation.uniqueId = 1    
map.addAnnotation(annotation)
let annotation2 = MyAnnotation()
annotation2.coordinate = location2
annotation2.title = "Zaid "
annotation2.subtitle = "aljameaa"
//Set uniqueId for annotation
annotation2.uniqueId = 2    
map.addAnnotation(annotation2)

现在calloutAccessoryControlTapped您点击的注释的方法中检查此uniqueId

func mapView(_ mapView: MKMapView, annotationView view:  MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    if (control as? UIButton)?.buttonType ==   UIButtonType.detailDisclosure {
        mapView.deselectAnnotation(view.annotation, animated: false)
        if let myAnnotation = view.annotation as? MyAnnotation {
            if (myAnnotation.uniqueId == 1) {
                 performSegue(withIdentifier: "info1", sender: view)
            }
            else  {
                 performSegue(withIdentifier: "info2", sender: view)
            }
        }            
    }
}
您可以通过

创建两个MKPointAnnotation的子类来执行此操作,然后在委托的方法中可以执行此操作:

func mapView(_ mapView: MKMapView, annotationView view:  MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
if view is subClass1 {
    // do action for subclass 1
}
else if view is subClass2 {
   // do action for subClass 2
}
}

如果这解决了您的问题,请告诉我。

更新

你可以使委托的实现更简单,如以下示例:

class ClassA:MKPointAnnotation{
    func doActionWhenCalloutTapped(){
        //do some action
    }
}
class ClassB:ClassA{
    override func doActionWhenCalloutTapped(){
        //do some actions for annotation of type B
    }
}
class ClassC:ClassA{
    override func doActionWhenCalloutTapped(){
        //do some actions for annotation of type C
    }
}
func viewDidLoad(){
    super.viewDidLoad()
    let annotation = ClassB()
    //annotation.setCoordinate(location)
    annotation.coordinate = locations
    annotation.title = "Zaid Homes"
    annotation.subtitle = "Hay aljameaa"
    
    map.addAnnotation(annotation)
    
    let annotation2 = ClassC
    //annotation.setCoordinate(location)
    annotation2.coordinate = location2
    annotation2.title = "Zaid "
    annotation2.subtitle = "aljameaa"
    
    map.addAnnotation(annotation2)
}
func mapView(_ mapView: MKMapView, annotationView view:  MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    
    (view.annotation as! ClassA).doActionWhenCalloutTapped()
   
}

最新更新