如何在同一位置删除两个注释?

问题描述 投票:-1回答:1

我只希望在一个带有标题和字幕的地方添加一个注释。我面临的问题是我得到两个注释。默认注释和可以自定义注释。我只需要自定义注释。需要照顾的主要方法可能是:addAnnotation和委托方法。

这里是问题的图片enter image description here


import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController {

    let locationManager = CLLocationManager()
    let regionInMeters: Double = 10000

    let localMap: MKMapView = {
        let map = MKMapView()
        map.translatesAutoresizingMaskIntoConstraints = false
        return map
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        checkLocationService()
        addAnnotations()
    }
    private func setupUI() {
        setupConstraints()
    }


}

// MARK: constraints
extension ViewController {
    private func setupConstraints() {
        view.addSubview(localMap)
        NSLayoutConstraint.activate([
            localMap.topAnchor.constraint(equalTo: view.topAnchor),
            localMap.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            localMap.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            localMap.bottomAnchor.constraint(equalTo: view.bottomAnchor),
        ])
    }
}
extension ViewController: CLLocationManagerDelegate, MKMapViewDelegate {
// checking location service is available
    private func checkLocationService() {
        if CLLocationManager.locationServicesEnabled() {
            setupLocationManager()
            checkLocationAuthorization()
        } else {

        }
    }
    private func checkLocationAuthorization() {
        switch CLLocationManager.authorizationStatus() {
        case .authorizedWhenInUse:
            print("Yesss")
            localMap.showsUserLocation = true
            centerViewOnUserLocation()
            //locationManager.startUpdatingLocation()
            break
        case .denied:
            break
        case .notDetermined:
            locationManager.requestWhenInUseAuthorization()
            break
        case .restricted:
            break
        case .authorizedAlways:
            break
        @unknown default:
            fatalError()
        }
    }

    private func setupLocationManager() {
        localMap.delegate = self
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
    }

    private func centerViewOnUserLocation() {
        if let location = locationManager.location?.coordinate {
            let region = MKCoordinateRegion(center: location, latitudinalMeters: regionInMeters, longitudinalMeters: regionInMeters)
            localMap.setRegion(region, animated: true)
        }

    }


// Delegate methods
    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(center: center, latitudinalMeters: regionInMeters, longitudinalMeters: regionInMeters)
        localMap.setRegion(region, animated: true)
    }

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        checkLocationAuthorization()
    }
// MARK: annotation
    private func addAnnotations() {
        let restaurantAnnotation = MKPointAnnotation()
        restaurantAnnotation.title = "FOOD BROTHER"
        restaurantAnnotation.subtitle = "Best burger in town"
        restaurantAnnotation.coordinate = CLLocationCoordinate2D(latitude: 52.37085, longitude: 9.732710)
        localMap.addAnnotation(restaurantAnnotation)
    }
    // https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=2ahUKEwijvYel7NTlAhVMjqQKHWeiChAQFjAAegQICBAB&url=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F49020023%2Fmapkit-annotations-disappearing&usg=AOvVaw2G13fjRVWs3b49cLQTjG_I

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        let reuseIdentifier = "annotationView"
        if annotation is MKUserLocation {
            return nil
        }
        var view = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier)
        if #available(iOS 11.0, *) {
            if view == nil {
                view = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
            }
            view?.displayPriority = .required
        } else {
            if view == nil {
                view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
            }
        }
        let pinImage = UIImage(named: "restaurantsIcon.png")
        let size = CGSize(width: 50, height: 50)
        UIGraphicsBeginImageContext(size)
        pinImage!.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
        let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
        view?.image = resizedImage
        view?.annotation = annotation
        view?.canShowCallout = true
        return view
    }
}
ios swift annotations
1个回答
0
投票

我找到了解决该问题的另一种方法。这是:

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    let reuseIdentifier = "annotationView"
    if annotation is MKUserLocation {
        return nil
    }
    var view = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
    view.glyphImage = UIImage(named: "restaurantsIcon")

    view.markerTintColor = .systemPink
    view.displayPriority = .required



    view.annotation = annotation
    view.canShowCallout = true
    return view
}
© www.soinside.com 2019 - 2024. All rights reserved.