iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios11 MKAnnotation没有显示两个注释大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
当我将注释添加到地图时,它们有时会显示,有时不会取决于它们彼此之间的距离.如果他们在同一个房子里,就说一个人不会出现.我如何让他们两个显示?我是否需要制作自定义注释类?我听说ios11有一个丛集功能,我需要使用它吗?这是代码(删节):

import UIKit
import MapKit
import Firebase

class 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 = false

var ref: FIRDatabaseReference!
let uid = FIRAuth.auth()!.currentUser!.uid

var timer = Timer()
var timeLeft = 0.0
var 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.@R_607_10613@estAlwaysAuthorization()
    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: - CLLOCATIOnManagerDelegate
extension GameViewController: CLLOCATIOnManagerDelegate {
  func LOCATIOnManager(_ manager: CLLOCATIOnManager,didupdateLOCATIOns 
  LOCATIOns: [CLLOCATIOn]) {
    let LOCATIOn = LOCATIOns.last as! CLLOCATIOn
    self.LOCATIOns.append(LOCATIOn)
}
}

解决方法

在MKAnnotationView上,您必须将R_671_11845@KFeaturedisplayPriority设置为“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没有显示两个注释所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。