【问题标题】:How do I use the default blue annotation for user location rather than my custom annotation?如何将默认蓝色注释用于用户位置而不是我的自定义注释?
【发布时间】:2020-10-25 09:04:56
【问题描述】:

我的地图上有自定义注释,现在我希望能够显示用户位置以及自定义注释。但是,当我显示用户位置时,它使用自定义注释而不是默认的蓝色注释。有没有办法让用户位置使用默认值?

我使用以下内容显示用户位置:

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
}

谢谢

【问题讨论】:

  • 如果您不想使用自定义注释,为什么不删除?
  • 我想对除显示用户位置的注释之外的所有注释使用我的自定义注释 - 当我调用 showUserLocation 它自然会使用此自定义注释
  • 那么您可以使用不同的自定义注释吗?
  • 我发现我需要检查进入 viewFor 的注释以检查它是否属于 MKUserLocation 类型 - 如果是,那么我只需返回 nil。
  • 它是通过以下方式实现的:stackoverflow.com/questions/61785563/…

标签: swift mapkit mkannotation


【解决方案1】:

如下所示:

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!)
}

【讨论】:

    【解决方案2】:

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

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

    这将阻止自定义您的用户位置

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-02
      • 2014-08-15
      • 1970-01-01
      • 2010-12-09
      • 2013-04-17
      • 2016-08-24
      • 1970-01-01
      相关资源
      最近更新 更多