微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

什么时候在Swift中调用mapView:viewForAnnotation?

根据 this post,每当调用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
    }

解决方法

简而言之,当注释落在地图视图的可见部分内时,将调用viewForAnnotation.即,(a)注释已被添加到地图视图中并且它与该区域一起下降或者(b)该区域改变以使得之前不可见的注释是.

顺便说一句,dataTaskWithURL的completionHandler将不在主线程上.必须将任何UI更新显式地分派回主线程,例如

dispatch_async(dispatch_get_main_queue()) {
    for place in places {
        //...
        placenum = placenum + 1
        self.map.addAnnotation(annotation)
    }

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

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

顺便说一句,请记住,如果您在地图上显示用户位置,那么它本身会导致调用viewForAnnotation.因此,如果您只看到它被调用一次,您可能需要确认注释是MKUserLocation还是您添加的注释之一.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐