ios - Swift 3 - MapKit 注释触摸监听器

标签 ios swift3 annotations mapkit

我到处找,没找到。我想采取以下行动。

当我触摸 map 上的注释时,我想更改 View 上的文本。

我试过下面的代码,但这不起作用。单击注释图钉时,我只是更改屏幕上的文本。

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()       
 }

最佳答案

我认为你应该将 delegate func 更改为

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

并在 viewDidLoad 添加 map.delegate = self

override func viewDidLoad() {
    super.viewDidLoad()

    map.delegate = self 
    .....
}

关于ios - Swift 3 - MapKit 注释触摸监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42708336/

相关文章:

ios - JSON "Any"不可转换为 'NSDictionary' 以及如何确定转换类型

java.net.URLClassLoader 不添加类注释

ios - 是否可以在 havingPredicate 中使用 group by count 来获取 CoreData(用于欺骗检测)?

ios - 品牌 Assets 的大小应该是多少?

ios - 更改应用语言并更新版本

ios - 使用 spritekit swift 在 swift 中创建游戏主菜单

Cocoa 不调用 ViewController 函数

ios - 如何获取 map 的缩放级别?

java - 在Android上的注释中使用XmlPullParser

iphone - 在 4.3.3 设备上从 xcode 构建/运行 IOS 5.0