swift - 什么时候是mapView :viewForAnnotation called in Swift?

标签 swift mapkit mapkitannotation

根据 this post每当您调用 addAnnotation 方法时,都会调用 mapView:viewForAnnotation

但是下面的代码只调用了一次mapView:viewForAnnotation。我有几个注释,但“ View 调用”只打印了一次。我想这与线程有关?

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, UITextFieldDelegate, MKMapViewDelegate, CLLocationManagerDelegate {

    @IBOutlet var searchtext: UITextField!
    @IBOutlet var map: MKMapView!

    var locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.searchtext.delegate = self

        locationManager.delegate = self
        self.map.delegate = self

        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
    }

    func textFieldDidBeginEditing(textField: UITextField!) {    //delegate method
        var allAnnotations = self.map.annotations
        self.map.removeAnnotations(allAnnotations)
    }

    func textFieldShouldReturn(textField: UITextField!) ->Bool {
        textField.resignFirstResponder()

        //...

        let session = NSURLSession.sharedSession()

        var task = session.dataTaskWithURL(url!, completionHandler: { (date, response, error) -> Void in
            if (error != nil) {
                println(error)
            }else {
                var placenum = 0
                //placenum is to see if all the places are in the visible rect. If so I will use showAnnotations to zoom (in) to the best-fitted view show them. If not, I will only show these in the visible rect
                for place in places {
                    //...
                    if (regionContains(self.map.region, coordinate)) {
                        placenum = placenum+1
                    }
                    self.map.addAnnotation(annotation)
                }

                if (placenum==places.count) {
                    self.map.showAnnotations(self.map.annotations, animated: true)
                }
            }

        })

        task.resume()

        return true
    }

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {

        //...
        self.map.setRegion(region, animated: false)

        locationManager.stopUpdatingLocation()
    }

    func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
        println("view called")

        return nil
    }

最佳答案

简而言之,当注释落在 map View 的可见部分时,将调用 mapView(_:viewFor:)。即,(a) 注释已添加到 map View 并且它属于 region 或 (b) region 更改,使得以前不存在的注释现在可见。不用说,如果您设置 map View 的委托(delegate)(以编程方式或在 Interface Builder 中),此方法也只会被调用。

顺便说一句,dataTask(with:completionHandler:) 的完成处理程序不会在主线程上调用。因此,任何 UI 更新都必须显式分派(dispatch)回主线程,例如

DispatchQueue.main.async {
    for place in places {
        //...
        placenum = placenum + 1
        self.map.addAnnotation(annotation)
    }

    self.map.showAnnotations(self.map.annotations, animated: true)
}

我建议确保与 map View 的交互发生在主线程上,如上所示。

顺便说一句,请记住,如果您在 map 上显示用户位置,这本身会导致 mapView(_:viewFor:) 被调用。因此,如果您看到它只被调用一次,您可能需要确认 annotationMKUserLocation 还是您添加的注释之一。

关于swift - 什么时候是mapView :viewForAnnotation called in Swift?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29610381/

相关文章:

每次启动/重新运行应用程序时,iOS 文件路径都会发生变化

objective-c - 类型化的 Objective-C 集合到 Swift

iphone - 为什么 MKMapView 把我放到海里?

swift - 调用mapView(_ :viewFor:) when user location changes

swift - 自定义注释为所有不同类型的 POI 显示相同的图像

swift - 如何在 Swift 4.2 中的 Mapkit 上设置坐标轴

ios - 在 UIScrollView 中调整 View 大小

ios - 在 MapView 上渲染多条折线

swift - 如何动画 MKMapKit 注释删除

Swift:选项数组的快捷方式展开