在下图中,“我的位置”注释应为蓝色圆圈。相反,我得到了气球注释。我很确定它与最后一段代码有关,但我不知道如何解决。周围的注释很好-我已在地图上添加了这些地方。
我删除了不相关的代码段:
class ExploreViewController: UIViewController, UISearchBarDelegate {
@IBOutlet weak var exploreMapView: MKMapView!
let locationManger = CLLocationManager()
let regionInMeters: Double = 5000
override func viewDidLoad() {
super.viewDidLoad()
checkLocationServices()
getSchoolMarkers()
}
@IBAction func getCurrentLocation(_ sender: UIButton) {
centerViewOnUserLocation()
}
func setupLocationManager() {
locationManger.delegate = self
locationManger.desiredAccuracy = kCLLocationAccuracyBest
}
func centerViewOnUserLocation() {
if let userLocation = locationManger.location?.coordinate {
let userRegion = MKCoordinateRegion.init(center: userLocation, latitudinalMeters: regionInMeters, longitudinalMeters: regionInMeters)
exploreMapView.setRegion(userRegion, animated: true)
}
}
func checkLocationServices() {
if CLLocationManager.locationServicesEnabled() {
setupLocationManager()
exploreMapView.showsUserLocation = true
centerViewOnUserLocation()
locationManger.startUpdatingLocation()
}
}
func getSchoolMarkers() {
// Code for creating annotations removed
self.exploreMapView.addAnnotation(schoolMarker)
}
}
extension ExploreViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let userLocation = locations.last else {return}
let currentLocation = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude)
let userRegion = MKCoordinateRegion.init(center: currentLocation, latitudinalMeters: regionInMeters, longitudinalMeters: regionInMeters)
exploreMapView.setRegion(userRegion, animated: true)
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
checkLocationAuthorization()
}
}
extension ExploreViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
var view = exploreMapView.dequeueReusableAnnotationView(withIdentifier: "reuseIdentifier") as? MKMarkerAnnotationView
if view == nil {
view = MKMarkerAnnotationView(annotation: nil, reuseIdentifier: "reuseIdentifier")
}
view?.annotation = annotation
view?.displayPriority = .required
return view
}
}
答案 0 :(得分:2)
您需要为nil
返回MKUserLocation
才能获得默认的注释视图:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard !annotation is MKUserLocation else {
return nil
}
var view = exploreMapView.dequeueReusableAnnotationView(withIdentifier: "reuseIdentifier") as? MKMarkerAnnotationView
if view == nil {
view = MKMarkerAnnotationView(annotation: nil, reuseIdentifier: "reuseIdentifier")
}
view?.annotation = annotation
view?.displayPriority = .required
return view
}