2014-09-03 75 views
0

我試圖找到一種方法來添加一個鏈接到Swift MapFramework中的Annotation,這個鏈接應該將用戶轉發給WebView,據我所見我找不到任何方法添加一個「可觸摸」鏈接到一個註解字幕在註釋中的Swift鏈接 - > Webview

這是我的代碼,但

class CustomPointAnnotation: MKPointAnnotation { 
    var imageName: String! 
} 




var info1 = CustomPointAnnotation() 
info1.coordinate = CLLocationCoordinate2DMake(42, -84) 
info1.title = "Info1" 
info1.subtitle = "Subtitle" 
info1.imageName = "1.png" 

var info2 = CustomPointAnnotation() 
info2.coordinate = CLLocationCoordinate2DMake(32, -95) 
info2.title = "Info2" 
info2.subtitle = "Subtitle" 
info2.imageName = "2.png" 


func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { 
    if !(annotation is CustomPointAnnotation) { 
     return nil 
    } 

    let reuseId = "test" 

    var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) 
    if anView == nil { 
     anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId) 
     anView.canShowCallout = true 
    } 
    else { 
     anView.annotation = annotation 
    } 

    //Set annotation-specific properties **AFTER** 
    //the view is dequeued or created... 

    let cpa = annotation as CustomPointAnnotation 
    anView.image = UIImage(named:cpa.imageName) 

    return anView 
} 

是否有可能使用UIGestureRecognizer這個辦法?

我已經嘗試過像

var longpress = UILongPressGestureRecognizer(target: self, action: "newInformation:") 
      longpress.minimumPressDuration = 2.0 
      info1.addGestureRecognizer(longpress) 

但隨着「ViewController.CustomPointAnnotation沒有一個名爲addGestureRecognizer成員」

+0

許多問題你問的問題已經在[documentation](https://developer.apple.com/library/ios/navigation/)或SO上得到了解答。在文檔中,搜索MKMapView類的引用或閱讀[位置和地圖編程指南](https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/Introduction/Introduction.html)。 SO上的答案目前主要在Objective-C中,但在Swift中完全相同(只需要翻譯語言)。 – Anna 2014-09-03 11:37:49

+1

對於這個當前的問題,典型的方法是通過設置視圖的leftCalloutAccessoryView或rightCalloutAccessoryView來添加標註附件按鈕。以http://stackoverflow.com/questions/25202613/mapkit-in-swift-part-2爲例。點擊按鈕時,地圖視圖將調用calloutAccessoryControlTapped委託方法。在那裏,您可以呈現網絡視圖。 – Anna 2014-09-03 11:39:47

+0

謝謝安娜的幫助,我對這個問題感到抱歉,我只是開始學習迅速和客觀的c,有時即使我看到答案我沒有得到它;)! – 2014-09-03 12:00:42

回答

0

這裏結束是一個有效的解決方案

override func viewDidLoad() { 
     super.viewDidLoad() 
      //1 

      var lat1:CLLocationDegrees = 40.748708 
      var long1:CLLocationDegrees = -73.985643 
      var latDelta1:CLLocationDegrees = 0.01 
      var longDelta1:CLLocationDegrees = 0.01 

      var span1:MKCoordinateSpan = MKCoordinateSpanMake(latDelta1, longDelta1) 
      var location1:CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat1, long1) 
      var region1:MKCoordinateRegion = MKCoordinateRegionMake(location1, span1) 

      Map.setRegion(region1, animated: true) 


      var info1 = CustomPointAnnotation() 
      info1.coordinate = location1 
      info1.title = "Test Title1!" 
      info1.subtitle = "Subtitle1" 
      info1.imageName = "1.png" 

      Map.addAnnotation(info1) 



      //2 


      var lat2:CLLocationDegrees = 41.748708 
      var long2:CLLocationDegrees = -72.985643 
      var latDelta2:CLLocationDegrees = 0.01 
      var longDelta2:CLLocationDegrees = 0.01 

      var span2:MKCoordinateSpan = MKCoordinateSpanMake(latDelta2, longDelta2) 
      var location2:CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat2, long2) 
      var region2:MKCoordinateRegion = MKCoordinateRegionMake(location2, span2) 




      var info2 = CustomPointAnnotation() 
      info2.coordinate = location2 
      info2.title = "Test Title2!" 
      info2.subtitle = "Subtitle2" 
      info2.imageName = "2.png" 
      Map.addAnnotation(info2) 



    } 

    func mapView(mapView: MKMapView!, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { 

     if control == annotationView.rightCalloutAccessoryView { 
      println("Disclosure Pressed! \(self.title)") 
     } 
    } 






    func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { 
     if !(annotation is CustomPointAnnotation) { 
      return nil 
     } 

     let reuseId = "test" 

     var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) 
     if anView == nil { 
      anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId) 
      anView.canShowCallout = true 
      anView.rightCalloutAccessoryView = UIButton.buttonWithType(.InfoDark) as UIButton 
     } 
     else { 
      anView.annotation = annotation 
     } 

     //Set annotation-specific properties **AFTER** 
     //the view is dequeued or created... 

     let cpa = annotation as CustomPointAnnotation 
     anView.image = UIImage(named:cpa.imageName) 

     return anView 
    }