swift 如何使用默认的蓝色注解来表示用户位置,而不是我的自定义注解?

1cosmwyk  于 5个月前  发布在  Swift
关注(0)|答案(3)|浏览(76)

我的Map上有自定义注解,现在我希望能够显示用户位置以及自定义注解。但是,当我显示用户位置时,它使用自定义注解而不是默认的蓝色注解。是否有方法使用户位置使用默认值?
我得到用户的位置显示使用下面的:

locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
map.showsUserLocation = true

字符串
自定义注解使用以下设置:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    let reuseIdentifier = "pin"
    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier)

    if annotationView == nil {
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
        annotationView?.canShowCallout = false
    } else {
        annotationView?.annotation = annotation
    }

    annotationView?.image = UIImage(named: "Marker")

    return annotationView
}


谢谢

h7wcgrx3

h7wcgrx31#

像下面这样做:

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    yourCustomAnnotation = CustomPointAnnotation()
    yourCustomAnnotation.pinCustomImageName = "Marker"
    yourCustomAnnotation.coordinate = location

    yourCustomAnnotationView = MKPinAnnotationView(annotation: yourCustomAnnotation, reuseIdentifier: "pin")
    map.addAnnotation(yourCustomAnnotationView.annotation!)
}

字符串

qybjjes1

qybjjes12#

我在处理同样的问题。在“viewFor annotation”委托方法的顶部使用这个检查:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        guard annotation as? MKUserLocation != mapView.userLocation else { return }

字符串
这将防止自定义您的用户位置

evrscar2

evrscar23#

你可以在MKMapViewDelegate中这样做:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation { return nil }
    
    // return custom views for other annotations...
}

字符串

相关问题