Navigation mit wechselndem Ziel

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Navigation mit wechselndem Ziel

    Servus,
    ich habe eine Karte mit mehreren Pins und will jedem Pin einen Navigationsbutton hinzufügen.
    Jetzt habe ich das Problem, dass so wie ich das kenne, bestimmte Koordinaten und ein bestimmter Titel im Code eingetragen werden muss. Bei mir ständen hier jedoch Variablen. Bis jetzt hab ich es noch nicht hingekiregt, die Variablen, welche am Anfang in einer for-loop für jeden Pin neu beschrieben werden in meiner Navigationsfunktion abzurufen.

    meine for-loop schaut so aus:

    Quellcode

    1. for location in locations {
    2. let annotation = customAnnotation()
    3. annotation.title = location.title
    4. annotation.address = location.address
    5. annotation.coordinate = CLLocationCoordinate2D (latitude: location.latitude, longitude: location.longitude)


    hier baue ich meinen Navigationsbutton:

    Quellcode

    1. func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    2. guard let annot = annotation as? customAnnotation else { return nil }
    3. if annotation is MKUserLocation {
    4. return nil
    5. }
    6. let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "customAnnotation")
    7. annotationView.image = UIImage(named: annot.icon)
    8. annotationView.canShowCallout = true
    9. let rightButton = UIButton(type: .detailDisclosure)
    10. rightButton.setImage(UIImage(named: annot.isCollapsed ? "ic_showmore" : "ic_showless"), for: .normal)
    11. annotationView.rightCalloutAccessoryView = rightButton
    12. if (annot.isCollapsed) {
    13. annotationView.detailCalloutAccessoryView = nil
    14. }
    15. else {
    16. //create NavigationButton
    17. let naviButton = UIButton(type: UIButtonType.custom) as UIButton
    18. naviButton.setTitle(annot.address, for: .normal)
    19. naviButton.setTitleColor(.black, for: .normal)
    20. naviButton.titleLabel?.numberOfLines = 0
    21. naviButton.titleLabel?.font = UIFont.italicSystemFont(ofSize: 14.0)
    22. naviButton.frame = CGRect(x: 0, y: 0, width: 200, height: 21)
    23. naviButton.backgroundColor = .white
    24. naviButton.addTarget(self, action: #selector(naviButtonAction), for: .touchUpInside)
    25. annotationView.detailCalloutAccessoryView = naviButton
    26. naviButton.widthAnchor.constraint(lessThanOrEqualToConstant: naviButton.frame.width).isActive = true
    27. naviButton.heightAnchor.constraint(lessThanOrEqualToConstant: 90.0).isActive = true
    28. }
    29. return annotationView
    Alles anzeigen

    und hier baue ich meine @ObjC funk naviButtonAction:

    Quellcode

    1. @objc func naviButtonAction(sender: UIButton) {
    2. let latitude: CLLocationDegrees = 48.147989
    3. let longitude: CLLocationDegrees = 11.617287
    4. let regionDistance: CLLocationDistance = 1000;
    5. let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
    6. let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance)
    7. let options = [MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center), MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span)]
    8. let placemark = MKPlacemark(coordinate: coordinates)
    9. let mapItem = MKMapItem(placemark: placemark)
    10. mapItem.name = "B"
    11. mapItem.openInMaps(launchOptions: options)
    12. }
    Alles anzeigen
    meine Lösungsansätze hingen Immer damit zusammen, dass ich genauso wie in der mapView-funktion versucht habe eine guard let-variable zu erzeugen, welches in mapView funktioniert, wo es um das icon geht
    das hat bis jetzt jedoch nicht funktioniert.

    Ich freu mich über jegliche Lösungsansätze und -vorschläge und stehe selbstverständlich für Fragen offen.
    Mit besten Grüßen,
    Gus :thumbup: