Creating a Custom Annotation View in iOS UIKit MapKit (UIKit MapKit Customization)

32 Views Asked by At

I have created a separate XIB file for the custom view called callout to connect the outlet.

import UIKit

class callout: UIView {
private let annotation: Annotation

    @IBOutlet weak var lb1: UILabel!
    
    init(annotation: Annotation) {
        self.annotation = annotation
        super.init(frame: .zero)
        setupView()
      }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupView() {
        lb1.text = "abb"

The problem lies here I have hardcoded value even that it is giving nil value error here.

Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

    }

}

import Foundation
import MapKit
class Annotation: NSObject, MKAnnotation {
public let placeName: String

    let coordinate: CLLocationCoordinate2D
    
    init(placeName: String,  coordinate: CLLocationCoordinate2D) {
        self.placeName = placeName
       
        self.coordinate = coordinate
        super.init()
    }

}

As I am building the code part by part I used a button name test only to confirm if the custom Annotation is working or not but it is not working sadly

\*\*MapKit class

import UIKit
import MapKit

class IOSMapViewController: UIViewController,CLLocationManagerDelegate,MKMapViewDelegate {

    @IBOutlet weak var mapView:MKMapView!
    var manager = CLLocationManager()
    //Class to get location
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        manager.delegate = self
        mapView.delegate = self
    
        manager.requestWhenInUseAuthorization()
        manager.startUpdatingLocation()
    }
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if let cLocation = locations.first {
            manager.stopUpdatingLocation()
            print("lat : \(cLocation.coordinate.latitude), long : \(cLocation.coordinate.longitude)")
            
            let loc = CLLocationCoordinate2D(latitude: cLocation.coordinate.latitude, longitude: cLocation.coordinate.longitude)
            let span = MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)
            
            let region = MKCoordinateRegion(center: loc, span: span)
            
            mapView.setRegion(region, animated: true)
            
            let marker = MKPointAnnotation()
            marker.coordinate = cLocation.coordinate
            mapView.addAnnotation(marker)
            
        }
    }
    
    @IBAction func **test**(_ sender: Any) {
        addAnnonations()
        
    }
    func **addAnnonations**()
    {
        let coordinate = CLLocationCoordinate2D(latitude: 40.741895, longitude: -73.989308)
    
        let customAnnotation = Annotation(placeName: "abc", coordinate: CLLocationCoordinate2D(latitude: 40.741895, longitude: -73.989308))
        mapView.addAnnotation(customAnnotation)
        mapView.setRegion(MKCoordinateRegion(center: coordinate, span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)), animated: true)
      
        mapView.register(AnnotationView.self,
                           forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
        mapView.showAnnotations([customAnnotation], animated: true)
        
        
    }

}

Creating a Custom Annotation View in iOS UIKit MapKit This is the article I am following to design my codehttps://betterprogramming.pub/how-to-implement-a-custom-mapkit-callout-in-ios-e452cda98278

But the major different is I made a custom callout class using callout.xib in IOSMapViewController When I click on test button A custom card like annotation should be display.

later on I will use it above markers.But whenever I click on button optional nil value exception comes callout class setup function line lb1.text = "abb"

0

There are 0 best solutions below