0

I try to find a way to add a link to a Annotation in Swift MapFramework, this link should forward the user to a WebView, as far i see i can't find any way to add a "touchable" link into a Annotations SubTitle

Here is my Code yet

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
}

Is there maybe a way to use the UIGestureRecognizer for this?

I already tried it like

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

But ending with "ViewController.CustomPointAnnotation does not have a member named addGestureRecognizer"

Fabian Boulegue
  • 6,476
  • 14
  • 47
  • 72
  • Many of the questions you are asking are already answered either in the [documentation](https://developer.apple.com/library/ios/navigation/) or on SO. In the docs, search for MKMapView class reference or read the [Location and Maps Programming Guide](https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/Introduction/Introduction.html). The answers on SO at the moment will mostly be in Objective-C but will work _exactly_ the same in Swift (just need to translate the language). –  Sep 03 '14 at 11:37
  • 1
    For this current question, the typical approach is to add a callout accessory button by setting the view's leftCalloutAccessoryView or rightCalloutAccessoryView. See http://stackoverflow.com/questions/25202613/mapkit-in-swift-part-2 for an example. When the button is tapped, map view will call the calloutAccessoryControlTapped delegate method. In there, you can present the web view. –  Sep 03 '14 at 11:39
  • Thanks Anna for the help, i m sorry for that question i just startet learning swift and objectiv c and sometimes even if i see the answere i dont get it ;)! – Fabian Boulegue Sep 03 '14 at 12:00
  • OK, no problem. Try making the changes like in the linked question. If you get it working, you can answer this question yourself. Otherwise, update this question with the new code and problem. –  Sep 03 '14 at 12:04
  • Small added question Anna (or should i start a new topic for that) is there a way to see what "Annotation" is selected? i tried it with self.title to get the title of info1/info2 – Fabian Boulegue Sep 03 '14 at 12:08
  • In calloutAccessoryControlTapped, the annotation selected is `annotationView.annotation`. To get access to custom properties, etc, you'll need to do something similar to what was done in viewForAnnotation (check the type using `is`, cast it using `as`, etc). –  Sep 03 '14 at 12:10
  • oki thanks ... ll try to get this somehow done even if its looks ways to complex for my skills yet :( – Fabian Boulegue Sep 03 '14 at 12:15

1 Answers1

0

Here is a working solution

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
    }
Fabian Boulegue
  • 6,476
  • 14
  • 47
  • 72