繁体   English   中英

ios11 MKAnnotation没有显示两个注释

[英]ios11 MKAnnotation not showing both annotations

当我将注释添加到地图时,它们有时会显示,有时不会取决于它们彼此之间的距离。 如果他们在同一个房子里,就说一个人不会出现。 我如何让他们两个显示? 我是否需要制作自定义注释类? 我听说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.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设置为“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中的新功能”解释了更多选项。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM