【问题标题】:Swift link in a Annotation -> Webview注释中的 Swift 链接 -> Webview
【发布时间】:2014-09-03 08:27:12
【问题描述】:

我试图找到一种方法在 Swift MapFramework 中添加指向注释的链接,此链接应将用户转发到 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 的成员”结尾

【问题讨论】:

  • 您提出的许多问题已经在documentation 或 SO 上得到解答。在文档中,搜索 MKMapView 类参考或阅读Location and Maps Programming Guide。目前关于 SO 的答案主要在 Objective-C 中,但在 Swift 中的工作完全相同相同(只需要翻译语言)。
  • 对于当前这个问题,典型的做法是通过设置视图的leftCalloutAccessoryView或者rightCalloutAccessoryView来添加一个callout附件按钮。有关示例,请参阅stackoverflow.com/questions/25202613/mapkit-in-swift-part-2。点击按钮时,地图视图将调用 calloutAccessoryControlTapped 委托方法。在那里,您可以展示网络视图。
  • 感谢 Anna 的帮助,对于这个问题,我很抱歉,我刚开始学习 swift 和 objectiv c,有时即使我看到了答案,我也不明白 ;)!
  • 好的,没问题。尝试在链接问题中进行更改。如果你得到它的工作,你可以自己回答这个问题。否则,请使用新代码和问题更新此问题。
  • 小添加的问题 Anna(或者我应该为此开始一个新主题)有没有办法查看选择了什么“注释”?我尝试使用 self.title 来获取 info1/info2 的标题

标签: map webview hyperlink swift annotations


【解决方案1】:

这是一个可行的解决方案

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
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-22
    • 2017-03-08
    • 1970-01-01
    • 2020-07-30
    • 1970-01-01
    • 2021-12-27
    • 2017-09-23
    • 1970-01-01
    相关资源
    最近更新 更多