2017-03-10 40 views
0

我到處搜索,但找不到它。我想在行動之下。Swift 3 - MapKit註解Touch Listener

當我觸摸地圖上的註釋時,我想更改視圖上的文本。

我嘗試了下面的代碼,但這不起作用。當單擊註釋針時,我只需更改屏幕上的文本。

private func mapView(mapView: MKMapView, didSelect view: MKAnnotationView{ 
    hastane_adi_text.text = "HAstane" 
} 

你可以看到我的 「ViewControllerClass」 下面。

import UIKit 
import MapKit 
import CoreLocation 

class HospitalControlloer: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate{ 

    @IBOutlet weak var hastane_adi_text: UILabel! 

    @IBOutlet weak var map: MKMapView! 

    @IBOutlet weak var randevu_al_button: UIButton! 
    @IBOutlet weak var hizala_button: UIButton! 

    let locationMenager = CLLocationManager() 


    override var preferredStatusBarStyle: UIStatusBarStyle { 

     return .lightContent 

    } 


    override func viewDidLoad() { 
     super.viewDidLoad() 


     randevu_al_button.layer.cornerRadius = 10; 
     hizala_button.layer.cornerRadius = 10; 



     let locationS:CLLocationCoordinate2D = CLLocationCoordinate2DMake(41.169425, 29.056801) 

     let sd = MKPointAnnotation() 

     sd.coordinate = locationS 
     sd.title = "Sarıyer Merkez Hastane" 


     let locationS2:CLLocationCoordinate2D = CLLocationCoordinate2DMake(41.097076, 29.05341) 

     let sd2 = MKPointAnnotation() 

     sd2.coordinate = locationS2 
     sd2.title = "Sarıyer Baltalimanı Hastane" 


     map.addAnnotation(sd) 
     map.addAnnotation(sd2) 

     self.locationMenager.delegate = self 

     self.locationMenager.desiredAccuracy = kCLLocationAccuracyBest 

     self.locationMenager.requestWhenInUseAuthorization() 

     self.locationMenager.startUpdatingLocation() 

     self.map.showsUserLocation = true 


     // Do any additional setup after loading the view. 


    } 


    private func mapView(mapView: MKMapView, didSelect view: MKAnnotationView){ 

     hastane_adi_text.text = "HAstane" 

    } 



    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    @IBAction func locate_button(_ sender: Any) { 

     locationMenager.requestLocation() 
    } 




    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 

     let location = locations.last 

     let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude) 

     let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)) 

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

     self.locationMenager.stopUpdatingLocation()  
} 
+0

嗨,功能'私人func mapView(地圖視圖:MKMapView,didSelect視圖:MKAnnotationView)'當你觸摸註釋時調用 –

+0

不,它不是調用怎麼做。我是ios新手。謝謝 – acanturgut

回答

2

我想你應該改變委託FUNC按鍵

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) { 
    hastane_adi_text.text = "HAstane" 
} 

,並在viewDidLoad

添加
override func viewDidLoad() { 
    super.viewDidLoad() 

    map.delegate = self 
    ..... 
} 
+0

謝謝你是工作! – acanturgut

0

你好,你需要實現這個方法func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView)並添加您的viewController作爲地圖代表self.map.delegate = self

我希望這可以幫助你

+0

謝謝你是工作! – acanturgut