Мои аннотации 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!)")
}
}
}