简体   繁体   English

Swift 3-MapKit注释触摸监听器

[英]Swift 3 - MapKit Annotation Touch Listener

I searched on everywhere, but i could not find it. 我到处搜寻,但找不到。 I want to below action. 我要采取以下行动。

When I touch the annotation on the map, I want to change text on the view. 当我触摸地图上的注释时,我想更改视图上的文本。

I tried below code but this does not work. 我尝试了下面的代码,但这不起作用。 I simply change text on the screen when annotation pin clicked. 单击注释图钉时,我只是在屏幕上更改文本。

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

You can see my "ViewControllerClass" below. 您可以在下面看到我的“ 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()       
 }

I think you should change delegate func to 我认为您应该将委托func更改为

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

and add map.delegate = self at viewDidLoad 并在viewDidLoad处添加map.delegate = self

override func viewDidLoad() {
    super.viewDidLoad()

    map.delegate = self 
    .....
}

Hello you need to implement this method func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) and add your viewController as map delegate self.map.delegate = self 您好,您需要实现此方法func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView)并将您的viewController添加为地图委托self.map.delegate = self

I hope this helps you 我希望这可以帮助你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM