pins not showing up in mapView. Swift 5

ⅰ亾dé卋堺 提交于 2019-12-13 04:26:05

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/58001095/pins-not-showing-up-in-mapview-swift-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!