gpt4 book ai didi

ios11 MKAnnotation 不显示两个注释

转载 作者:搜寻专家 更新时间:2023-10-30 23:08:02 24 4
gpt4 key购买 nike

当我将注释添加到 map 时,它们有时会显示,有时不会显示,具体取决于它们彼此之间的距离。如果他们在同一所房子里,可以说一个不会出现。我如何让他们两个都显示?我需要制作自定义注释类吗?我听说 ios11 有一个结 block 功能,我需要使用它吗?这是代码(删节):

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.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: - CLLocationManagerDelegate
extension GameViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations
locations: [CLLocation]) {
let location = locations.last as! CLLocation
self.locations.append(location)
}
}

最佳答案

在 MKAnnotationView 上,您必须将 MKFeatureDisplayPriority 设置为“必需”。您可以通过实现 MKMapViewDelegate 和 mapView(MKMapView, viewFor: MKAnnotation) 来修改注释 View 。像这样:

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 不显示两个注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47637352/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com