如何在Swift中删除我的位置的注释?我只想要一个脉动点

我正面临一个问题。我只想显示我真正添加的注释。因此,以我为例,我想找回我的脉动点。不是注释。我希望有人能帮助我。

我认为要查看的主要方法是委托方法和addAnnotations方法。

这是我的问题的照片: double annotations and annotation my current location

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,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"
        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
    }
}
wodejita 回答:如何在Swift中删除我的位置的注释?我只想要一个脉动点

将以下代码添加到委托方法。

func mapView(_ mapView: MKMapView,viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    // Add 3 line code
    if annotation is MKUserLocation {
        return nil
    }
    ...
}
本文链接:https://www.f2er.com/3154190.html

大家都在问