【问题标题】:How to delay callout from showing when annotation selected in MKMapView? Swift 4如何在 MKMapView 中选择注释时延迟标注显示?斯威夫特 4
【发布时间】:2018-11-27 01:19:13
【问题描述】:

(这是我的第一个堆栈溢出问题哈哈)

更新: 从这个链接 - Center MKMapView BEFORE displaying callout

我从 Thermometer 实现了解决方案,但是选择和取消选择注释会使我的应用程序看起来出现故障。

我认为最好的方法是将 callOut(弹出详细信息)延迟几秒钟,以便地图有时间先移动。

这是我的代码:

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    guard let annotation = view.annotation else {
        return
    }

    let currentAnnotation = view.annotation as? MapMarker
    Global.currentAnnotation = currentAnnotation
    findRelatinoshipLines()

    if Global.showLifeStoryLines {
        var locations = lifeStoryAnnotations.map { $0.coordinate }
        let polyline = MKPolyline(coordinates: &locations, count: locations.count)
        Global.finalLineColor = Global.lifeStoryColor
        mapView.addOverlay(polyline)
    }

    if Global.showFatherLines {
        var fatherLocations = fatherTreeAnnotations.map { $0.coordinate }
        let fatherPolyline = MKPolyline(coordinates: &fatherLocations, count: fatherLocations.count)
        Global.finalLineColor = Global.fatherLineageColor

        mapView.addOverlay(fatherPolyline)
    }

    if Global.showMotherLines {
        var motherLocations = motherTreeAnnotations.map { $0.coordinate }
        let motherPolyline = MKPolyline(coordinates: &motherLocations, count: motherLocations.count)
        Global.finalLineColor = Global.motherLineageColor

        mapView.addOverlay(motherPolyline)
    }

    if Global.showSpouseLines {
        var locations = spouseAnnotations.map { $0.coordinate }
        let polyline = MKPolyline(coordinates: &locations, count: locations.count)
        Global.finalLineColor = Global.spouseColor
        mapView.addOverlay(polyline)
    }

    if Global.zoomChange == true {
        Global.zoomChange = false
    } else {
        let currentRegion = mapView.region
        let span = currentRegion.span
        let location = currentAnnotation!.coordinate
        let region = MKCoordinateRegion(center: location, span: span)

        DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
            mapView.setCenter(annotation.coordinate, animated: true)
            //mapView.setRegion(region, animated: true)
        }
    }
}

继续:

基本上,我正在开发一个家族谱系应用程序,该应用程序在地图上显示亲戚的事件。

当我单击注释(事件)时,详细信息(事件属于谁、何时何地等)会在上方弹出一个信息按钮以显示所选人员。

我已将其设置为设置MKMapView 区域,以便每次单击新注释时所选注释居中。

问题是当我单击屏幕边缘的事件时,我的注释标题/说明会居中弹出,以便它适合我的屏幕,因为它不知道我打算重新居中围绕所述注释的地图视图。

我想知道是否有任何方法可以使标题/描述直接显示在所选注释上方的中心,以便在我移动地图时所有内容都居中并适合屏幕。

以下是我所说的一些截图:

Before and After

【问题讨论】:

  • 如果您显示与问题相关的代码会有所帮助。
  • 您应该在弹出详细信息之前将地图居中。
  • @KosukeOgawa 是的,我正在考虑借助以下链接:stackoverflow.com/questions/34397934/…
  • @KosukeOgawa 我在该链接上实现了 Thermometer 的解决方案,但是选择和取消选择注释会使我的应用程序看起来出现故障。你有什么其他方法可以延迟 showCallout(详情弹出)?
  • @MwcsMac 已更新。谢谢!

标签: swift annotations mkmapview callouts


【解决方案1】:

通过调用setCenter 解决了这个问题,mapView(_:didSelect:) 稍有延迟:

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    guard let annotation = view.annotation else {
        return
    }

    DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
        mapView.setCenter(annotation.coordinate, animated: true)
    }
}

【讨论】:

  • 我试过这个,而不是延迟弹出窗口 - 它延迟了地图居中。检查我的帖子更新以获取我的代码。
  • 这是因为我的注释是标记而不是大头针吗?
猜你喜欢
  • 1970-01-01
  • 2017-11-14
  • 1970-01-01
  • 1970-01-01
  • 2017-07-22
  • 2017-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多