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没有显示两个注释所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)