【问题标题】:Swift 3 - MapKit Annotation Touch ListenerSwift 3 - MapKit 注释触摸监听器
【发布时间】:2017-03-10 01:03:36
【问题描述】:

我到处搜索,但找不到。我想采取以下行动。

当我触摸地图上的注解时,我想更改视图上的文字。

我尝试了下面的代码,但这不起作用。单击注释针时,我只需更改屏幕上的文本。

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

【问题讨论】:

  • 您好,当您触摸注释时,函数private func mapView(mapView: MKMapView, didSelect view: MKAnnotationView) 调用了吗
  • 不,它不叫怎么做。我是ios新手。谢谢

标签: ios swift3 annotations mapkit


【解决方案1】:

我认为您应该将委托函数更改为

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

并在viewDidLoad处添加map.delegate = self

override func viewDidLoad() {
    super.viewDidLoad()

    map.delegate = self 
    .....
}

【讨论】:

    【解决方案2】:

    您好,您需要实现此方法func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) 并将您的viewController 添加为地图委托self.map.delegate = self

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-08
      • 1970-01-01
      • 1970-01-01
      • 2019-01-27
      • 2017-05-18
      • 1970-01-01
      • 2012-01-27
      相关资源
      最近更新 更多