图钉未显示在 mapView 中.斯威夫特 5 [英] pins not showing up in mapView. Swift 5

查看:29
本文介绍了图钉未显示在 mapView 中.斯威夫特 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在地图上显示图钉(注释).然后从pin到pin绘制多边形线(annotation to annotation)

我收到一个双精度数组,我将其转换为 CLLocationCoordiante2D.第一个 lat 和 long 值始终为 0.0,因此我将它们从数组中删除,因为我不希望那里出现任何问题.

我将双打映射到坐标,并将它们添加到 mapView

我还包含了一个 viewFor 函数,我认为我真的不需要?

地图不会缩放到任何位置,也不会显示图钉.我知道我需要对其进行编码,我需要所有引脚周围的一般半径.在引脚实际出现后,我会继续努力.

另外,我不在乎名字,我只想显示图钉.

我试过设置单个坐标,但仍然没有固定.

在 viewDidLoad() 中正确设置了 mapView 委托

我在调试器中记录了这些位置,它们显示正确.

func createAnnotations() {latitude.remove(at: 0)longitude.remove(at: 0)让坐标 = zip(纬度,经度).地图(CLLocationCoordinate2D.init)AppLogger.logInfo("\(坐标)")让注释 = zip(坐标,名称).map {(坐标,名称)->MKPoint 中的注解让注释 = MKPointAnnotation()annotation.coordinate = 坐标annotation.title = 名称返回注释}mapView.addAnnotations(注解)mapView.showAnnotations(注释,动画:真)}func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) ->MKAnnotationView?{守卫注解是 MKPointAnnotation else { return nil }让标识符 = "注释"var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)如果 annotationView == nil {annotationView = MKPinAnnotationView(注释:注释,reuseIdentifier:标识符)annotationView!.canShowCallout = true} 别的 {annotationView!.annotation = annotation}返回注解视图}

[__C.CLLocationCoordinate2D(纬度: 41.89454659591164, 经度: -87.67463844121563), __C.CLLocationCoordinate2D(纬度: 41.89424383424124, longitude) -8073) -8073

预期的结果是当显示 mapView 时,我们会看到引脚(注释),以及将它们从第一个引脚连接到最后一个引脚的多边形线.我可以稍后处理的绘制多边形.

解决方案

你是对的,如果你为你的注解注册注解视图,你不需要 viewFor,例如

mapView.register(MKPinAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier))

但让我们暂时搁置一下.让我们专注于未显示的注释.有两种可能:

  1. 确保您确实在添加坐标.

    您正在打印坐标.尝试打印注释.即,让我们确保名称不为空,因此注释为空.如果你 zip/map 一个非空数组和一个空数组,最终结果将是空的.(它被缩短到最小数组长度.)

  2. 确保您完全调用了 viewFor.

    假设您已经解决了第一个问题,请尝试在 viewFor 中添加断点或日志语句,并确保它被调用.例如.确保您设置了地图视图的委托等.

I need to display pins ( annotations ) on a map. Then draw polygon lines from pin to pin ( annotation to annotation )

I receive an array of Doubles that I convert to CLLocationCoordiante2D. the first lat and long values are always 0.0 so I remove those from the array because I don't want any issues there.

I map the doubles to the coordinates, and add them to the mapView

I also included a viewFor function with I thought I don't really need?

the map doesn't zoom to any location and NO pins are shown. I know I need to code that in, I will want a general radius around all the pins. Ill work on that after the pins actually show up.

Also, I don't care about names, I just want the pins to show up.

I have tried setting a single coordinate and still no pin.

The mapView delegate is correctly set in viewDidLoad()

I log the locations in debugger and they show up correct.

func createAnnotations() {
    latitude.remove(at: 0)
    longitude.remove(at: 0)

   let coordinates = zip(latitude, longitude).map(CLLocationCoordinate2D.init)

    AppLogger.logInfo("\(coordinates)")

   let annotations = zip(coordinates, names)
       .map { (coordinate, name) -> MKPointAnnotation in
           let annotation = MKPointAnnotation()

           annotation.coordinate = coordinate
           annotation.title = name

           return annotation
       }

   mapView.addAnnotations(annotations)
   mapView.showAnnotations(annotations, animated: true)
}


func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    guard annotation is MKPointAnnotation else { return nil }

    let identifier = "Annotation"
    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)

    if annotationView == nil {
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
        annotationView!.canShowCallout = true
    } else {
        annotationView!.annotation = annotation
    }
    return annotationView
}

[__C.CLLocationCoordinate2D(latitude: 41.89454659591164, longitude: -87.67463844121563), __C.CLLocationCoordinate2D(latitude: 41.89424383424124, longitude: -87.67461071330482))]

The expected result is when the mapView is shown, we see the pins ( annotations ), and polygon lines connecting them from first pin to last. the draw polygon I can work on later.

解决方案

You are correct that you don’t need viewFor if you register your annotation view for your annotation, e.g.

mapView.register(MKPinAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier))

But let’s set that aside for a moment. Let’s focus on the annotation not showing up. There are two possibilities:

  1. Make sure you’re actually adding coordinates.

    You’re printing coordinates. Try printing annotations. I.e., let’s make sure that names wasn’t empty and therefore annotations was empty. If you zip/map an non-empty array with an empty one, the final result will be empty. (It’s shortened to the minimum array length.)

  2. Make sure you viewFor is getting called at all.

    Assuming you’ve fixed the first issue, try adding breakpoint or log statement in viewFor and make sure it’s getting called. E.g. make sure you set the delegate of the map view, etc.

这篇关于图钉未显示在 mapView 中.斯威夫特 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆