[英]My Swift MapKit Annotations Aren't Showing
除非我一直放大,否則我的 Swift MapKit 注釋不會顯示。我不知道為什么,並做了一些研究。
這是我的控制器代碼。
注釋確實會顯示,但僅當您一直放大時才顯示。當我設置為任何區域和縮放級別時,如何顯示它們?
//
// DeliveryControllerMain.swift
// CO19
//
// Created by JJ Zapata on 3/20/20.
// Copyright © 2020 Jorge Zapata. All rights reserved.
//
import UIKit
import MapKit
import Firebase
import FirebaseCore
import FirebaseAuth
import CoreLocation
import FirebaseDatabase
class DeliveryControllerMain: UIViewController, CLLocationManagerDelegate {
@IBOutlet var mapView : MKMapView!
let locationManager = CLLocationManager()
var posts = [ListObject]()
override func viewDidLoad() {
super.viewDidLoad()
self.checkForLocationservices()
self.getPlaces()
// Do any additional setup after loading the view.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
self.getPlaces()
}
@IBAction func refrsh(_ sender: UIButton) {
self.getPlaces()
}
func getPlaces() {
if CheckInternet.Connection() {
posts.removeAll()
// let mapAnno = MKPointAnnotation()
// mapAnno.title = "asdf"
// mapAnno.coordinate = CLLocationCoordinate2D.init(latitude: 48.314084538662335, longitude: 11.55610970224152)
// self.mapView.addAnnotation(mapAnno)
// let allAnnotations = self.mapView.annotations
// self.mapView.removeAnnotations(allAnnotations)
let postRef = Database.database().reference().child("Open")
postRef.observe(.childAdded) { (snapshot) in
if let value = snapshot.value as? [String : Any] {
let user = ListObject()
user.id = value["postID"] as? String ?? "Not Found"
user.title = value["postTitle"] as? String ?? "Not Found"
user.author = value["postAuthor"] as? String ?? "Not Found"
user.list = value["postList"] as? String ?? "Not Found"
user.date = value["postDate"] as? String ?? "Not Found"
user.long = value["postLong"] as? String ?? "Not Found"
user.status = value["postStatus"] as? String ?? "Not Found"
user.lat = value["postLat"] as? String ?? "Not Found"
self.posts.append(user)
// MAKE COORDINATES HERE
let london = MKPointAnnotation()
Database.database().reference().child("Users").child(user.author!).child("name").observe(.value, with: { (data) in
let name : String = (data.value as? String)!
london.title = name
let long = Double(user.long!)
let lat = Double(user.lat!)
london.coordinate = CLLocationCoordinate2D(latitude: lat!, longitude: long!)
self.mapView.addAnnotation(london)
print(user.lat!)
print(user.long!)
})
}
print(self.posts.count)
print(self.posts)
self.posts.reverse()
// ACTION HERE
}
} else {
let alert = UIAlertController(title: "No Connection", message: "Please Be Connected To Internet To Access Your LockIt Accounts", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Okay", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
func setupLocationanager() {
self.locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
}
func checkAuthoritzation() {
switch CLLocationManager.authorizationStatus() {
case .authorizedWhenInUse:
mapView.showsUserLocation = true
self.centerViewOnUserLocation()
break
case .denied:
self.makeAlert(title: "Error", message: "Please Check Location Services Settings")
break
case .notDetermined:
locationManager.requestWhenInUseAuthorization()
break
case .restricted:
self.makeAlert(title: "Error", message: "Please Check Location Services Settings")
break
case .authorizedAlways:
mapView.showsUserLocation = true
break
default:
break
}
}
func centerViewOnUserLocation() {
if let location = locationManager.location?.coordinate {
let region = MKCoordinateRegion.init(center: location, latitudinalMeters: 1000, longitudinalMeters: 1000)
mapView.setRegion(region, animated: true)
}
}
func checkForLocationservices() {
if CLLocationManager.locationServicesEnabled() {
self.setupLocationanager()
self.checkAuthoritzation()
} else {
self.checkAuthoritzation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else { return }
let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
let region = MKCoordinateRegion.init(center: center, latitudinalMeters: 1, longitudinalMeters: 1)
mapView.setRegion(region, animated: true)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
func makeAlert(title: String, message: String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Okay", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
if let annotationTitle = view.annotation?.title {
print("User tapped on annotation with title: \(annotationTitle!)")
}
}
}
因此,我建議至少為您的注釋(和集群)定義注釋視圖,設置displayPriority
。 例如,如果使用MKMarkerAnnotationView
:
class PointOfInterestClusterAnnotation: MKMarkerAnnotationView {
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
displayPriority = .required
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override var annotation: MKAnnotation? {
didSet {
displayPriority = .required
}
}
}
class PointOfInterestAnnotation: MKMarkerAnnotationView {
static let clusteringIdentifier = "..."
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
displayPriority = .required
clusteringIdentifier = PointOfInterestAnnotation.clusteringIdentifier
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override var annotation: MKAnnotation? {
didSet {
clusteringIdentifier = PointOfInterestAnnotation.clusteringIdentifier
displayPriority = .required
}
}
}
顯然,選擇適合您的應用程序的注釋名稱。 但希望這說明了這個想法。
然后在viewDidLoad
,注冊這兩個注釋視圖:
mapView.register(PointOfInterestAnnotation.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
mapView.register(PointOfInterestClusterAnnotation.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultClusterAnnotationViewReuseIdentifier)
(如果面向 iOS 11 及更高版本,通常不需要mapView(_:viewFor:)
。)
如果你這樣做,當你縮小時,沒有注釋視圖應該消失,而是你應該享受聚類。 當您重新放大時,它們應該會散開並重新出現。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.