0

How add annotations to polyline and polygon in Swift & MapKit? By Point is simple.

Z.S.
  • 39
  • 1
  • 9

1 Answers1

1

S.,

I'm not sure what you're asking here, but I assume you want to display an annotation somewhere on the polyline.

First the intro how to get the the polyline: So, lets assume you have an array of CLLocation objects that will draw the polyline on the map. We call this array of location objects: myLocations and it's of type [CLLocation]. Now somewhere in your app you call a method that creates the polyline, we call this method createOverlayObject(locations: [CLLocation]) -> MKPolyline.

Your call could look like this:

let overlayPolyline = createOverlayObject(myLocations)

The method you called then could look like this:

func createOverlayObject(locations: [CLLocation]) -> MKPolyline {
    //This method creates the polyline overlay that you want to draw.
    var mapCoordinates = [CLLocationCoordinate2D]()

    for overlayLocation in locations {

        mapCoordinates.append(overlayLocation.coordinate)
    }

    let polyline = MKPolyline(coordinates: &mapCoordinates[0], count: mapCoordinates.count)

    return polyline
}

This was the first part, don't forget to implement the mapView(_: rendererForOverlay overlay:) to get the line rendered. this part could look something like this:

func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {
    //This function creatss the renderer for the polyline overlay. This makes the polyline actually display on screen.
    let renderer = MKPolylineRenderer(overlay: overlay)
    renderer.strokeColor = mapLineColor //The color you want your polyline to be.
    renderer.lineWidth = self.lineWidth

    return renderer
}

Now the second part get the annotation somewhere on the map. This is actually straight forward if you know what the coordinates are where you want to put your annotation. creating and displaying the annotation is straightforward again, assuming you have defined a map view called myNiceMapView:

func createAnnotation(myCoordinate: CLLocationCoordinate2D) {
    let myAnnotation = MKPointAnnotation()
    myAnnotation.title = "My nice title"
    startAnnotation.coordinate = myCoordinate

    self.myNiceMapView.addAnnotations([myAnnotation])
}

Don't forget to implement mapView(_: MKMapView, viewForAnnotation annotation:) -> MKAnnotationView? method, which might look like:

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    //This is the mapview delegate method that adjusts the annotation views.

    if annotation.isKindOfClass(MKUserLocation) {

        //We don't do anything with the user location, so ignore an annotation that has to do with the user location.
        return nil
    }

    let identifier = "customPin"
    let trackAnnotation = MKAnnotationView.init(annotation: annotation, reuseIdentifier: identifier)

    trackAnnotation.canShowCallout = true

    if annotation.title! == "Some specific title" { //Display a different image
        trackAnnotation.image = UIImage(named: "StartAnnotation")
        let offsetHeight = (trackAnnotation.image?.size.height)! / 2.0
        trackAnnotation.centerOffset = CGPointMake(0, -offsetHeight)
    } else { //Display a standard image.
        trackAnnotation.image = UIImage(named: "StopAnnotation")
        let offsetHeight = (trackAnnotation.image?.size.height)! / 2.0
        trackAnnotation.centerOffset = CGPointMake(0, -offsetHeight)
    }

    return trackAnnotation
}

Now the challenges is finding the right coordinate where to put your annotation. I can't find anything better than that you have a CLLocationCoordinate2D that references the location you want to put the annotation. Then with a for-in loop find the location where you want to put your annotation, something like this:

for location in myLocations {

  if (location.latitude == myReferenceCoordinate.latitude) && (location.longitude == myReferenceCoordinate.longitude) {
    self.createAnnotation(location: CLLOcationCoordinate2D)
  }
}

Hope this answers your question.

Kon
  • 4,023
  • 4
  • 24
  • 38
MacUserT
  • 1,760
  • 2
  • 18
  • 30