加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 04:36:46 所属栏目:百科 来源:网络整理
导读:根据 this post,每当调用addAnnotation方法时,都会调用mapView:viewForAnnotation 但是下面的代码只调用一次mapView:viewForAnnotation.我有几个注释,但“视图调用”只打印一次.我想这与线程有关? import UIKitimport MapKitimport CoreLocationclass Vie
根据 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还是您添加的注释之一.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读