4

根据这篇文章,每当您调用addAnnotation方法时, mapView:viewForAnnotation都会被调用

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

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
    }
4

2 回答 2

3

简而言之,mapView(_:viewFor:)当注释落入地图视图的可见部分时调用。即,要么 (a) 注释已添加到地图视图中,并且它与region或 (b)region更改有关,因此以前不可见的注释现在是。不用说,这个方法也只会在你设置地图视图的委托时被调用(通过编程方式或在界面生成器中)。

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

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

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

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

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

于 2015-04-13T16:56:03.210 回答
3

另外请确保将 mapview 的委托属性设置为 self。

mapview.delegate = self

您也可以通过使用 Connection Inspector (Interface Builder) 将 mapview 的代理出口连接到 ViewController 来做到这一点

于 2015-07-24T06:18:25.143 回答