2014-09-03 2 views
0

Я пытаюсь найти способ добавления ссылки на аннотацию в Swift MapFramework, эта ссылка должна перенаправлять пользователя в WebView, насколько я вижу, я не могу найти способ добавить «осязаемую» ссылку в аннотациях SubtitleSwift ссылка в аннотация -> Веб-просмотр

Вот мой код еще

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

Многие из вопросов вы спрашиваете, уже ответили либо в [документации] (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, но будут работать _exactly_ то же самое в Swift (просто нужно перевести язык). – Anna

+1

Для этого текущего вопроса типичным подходом является добавление кнопки аксессуара выноски путем установки вида leftCalloutAccessoryView или rightCalloutAccessoryView. Для примера см. Http://stackoverflow.com/questions/25202613/mapkit-in-swift-part-2. Когда кнопка нажата, отображение карты вызовет метод делегата calloutAccessoryControlTapped. Там вы можете представить веб-представление. – Anna

+0

Спасибо Анне за помощь, я сожалею об этом вопросе, и я просто начинаю учиться быстро и объективно, а иногда даже, если вижу, что я не получаю это;)! –

ответ

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 
    } 
Смежные вопросы