pins not showing up in mapView. Swift 5
Asked Answered
S

1

0

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.

Suzerainty answered 18/9, 2019 at 21:42 Comment(1)
I fixed it! I didn't create a Coordinate2D array! DOHHHHHHHH!!Suzerainty
F
0

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.

Forester answered 19/9, 2019 at 14:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.