ios11 MKAnnotation没有显示两个注释

ios11 MKAnnotation没有显示两个注释,第1张

概述当我将注释添加到地图时,它们有时会显示,有时不会取决于它们彼此之间的距离.如果他们在同一个房子里,就说一个人不会出现.我如何让他们两个显示?我是否需要制作自定义注释类?我听说ios11有一个丛集功能,我需要使用它吗?这是代码(删节): import UIKitimport MapKitimport Firebaseclass GameViewController: UIViewContro 当我将注释添加到地图时,它们有时会显示,有时不会取决于它们彼此之间的距离.如果他们在同一个房子里,就说一个人不会出现.我如何让他们两个显示?我是否需要制作自定义注释类?我听说ios11有一个丛集功能,我需要使用它吗?这是代码(删节):

import UIKitimport MapKitimport Firebaseclass GameVIEwController: UIVIEwController {@IBOutlet weak var mapVIEw: MKMapVIEw!fileprivate var locations = [CLLocation]()fileprivate var userLocations = [(loc: CLLocation,name: String,team: String)]()fileprivate var userAnnotations = [MKAnnotation]()fileprivate var hasBeenUP = falsevar ref: FIRDatabaseReference!let uID = FIRAuth.auth()!.currentUser!.uIDvar timer = Timer()var timeleft = 0.0var firstTimer = Timer()var name = ""var team = ""overrIDe func vIEwDIDLoad() {    super.vIEwDIDLoad()    let center = CLLocationCoordinate2D(latitude: 47.786769,longitude: -20.413634)    let region = MKCoordinateRegion(center: center,span: MKCoordinateSpan(latitudeDelta: 0.01,longitudeDelta: 0.01))    self.mapVIEw.setRegion(region,animated: true)    mapVIEw.mapType = .hybrID    locationManager.startUpdatingLocation()    ref = FIRDatabase.database().reference()    setupulsending()    getMetaInfo()    ref.child("realtimeLocations").observe(FIRDataEventType.value,with: { (snapshot) in        self.userLocations = []        for rest in snapshot.children.allObjects as! [FIRDataSnapshot] {            guard let snapshotValue = snapshot.value as? NSDictionary,let snapVal = snapshotValue[rest.key] as? NSDictionary else {                break            }            let name = snapVal["name"] as! String            let team = snapVal["team"] as? String ?? ""            if let lat = snapVal["lat"] as? Double,let long = snapVal["long"] as? Double {                let location = CLLocation(latitude: lat,longitude: long)                self.userLocations.append((loc: location,name: name,team: team))            }else {            }        }        dispatchQueue.main.async {            self.updateUserLocation()        }    })}private lazy var locationManager: CLLocationManager = {    let manager = CLLocationManager()    manager.allowsBackgroundLocationUpdates = true    manager.desiredAccuracy = kCLLocationAccuracyBest    manager.delegate = self    manager.requestAlwaysAuthorization()    return manager}()func updateUserLocation() {    for an in self.mapVIEw.annotations {        mapVIEw.removeAnnotation(an)    }    for loc in userLocations {        let annotation = MKPointAnnotation()        annotation.coordinate = loc.loc.coordinate        annotation.Title = loc.name        annotation.subTitle = "local"        mapVIEw.addAnnotation(annotation)    }}}// MARK: - CLLocationManagerDelegateextension GameVIEwController: CLLocationManagerDelegate {  func locationManager(_ manager: CLLocationManager,dIDUpdateLocations   locations: [CLLocation]) {    let location = locations.last as! CLLocation    self.locations.append(location)}}
解决方法 在MKAnnotationVIEw上,您必须将MKFeaturedisplayPriority设置为“required”.您可以通过实现MKMapVIEwDelegate和mapVIEw(MKMapVIEw,vIEwFor:MKAnnotation)来修改注释视图.像这样的东西:

func mapVIEw(_ mapVIEw: MKMapVIEw,vIEwFor annotation: MKAnnotation) -> MKAnnotationVIEw? {        if annotation is MKUserLocation { return nil }        var vIEw = mapVIEw.dequeueReusableAnnotationVIEw(withIDentifIEr: "yourIDentifIEr")        if vIEw == nil {            vIEw = MKMarkerAnnotationVIEw(annotation: nil,reuseIDentifIEr: "yourIDentifIEr")        }        vIEw?.displayPriority = .required        return vIEw}

WWDC 2017视频237“MapKit中的新功能”解释了更多选项.

总结

以上是内存溢出为你收集整理的ios11 MKAnnotation没有显示两个注释全部内容,希望文章能够帮你解决ios11 MKAnnotation没有显示两个注释所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1067864.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存