ios – A number of customized annotations from JSON file and just one sort of customized annotation seem on my map (market, uikit, swift)

0
64


I’ve an enormous problem from my UIKit venture.

I wish to show a variety of annotations from a JSONfile with a number of fashion of annotations. I take advantage of UIKit and MapKit. I took inspiration from TandM venture of Apple however that does not work for me and my head goes to blow up. I’ve solely the lastest fashion in my registrerAnnotationViewClasses who’re displayed

Thanks prematurely for everybody who assist me and have an incredible day.

So right here my class :

import MapKit
import UIKit

class Location: NSObject, Decodable, Identifiable, MKAnnotation {


var id: Int
var identify: String
var latitude: Double
var longitude : Double
var coordinate: CLLocationCoordinate2D {
    .init(latitude: latitude, longitude: longitude)
    
}

var title : String? {
    return identify
}




enum GargageType: Decodable {
    case rubbish
    case glass
    case separate
    case different
    
}
var sort : GargageType {
    
    if identify == "Verre" {
        return .glass
    }
    if identify == "Tri selectif" {
        return .separate
    }
    if identify == "Ordures ménagères"{
        return .rubbish
    }
    else {
        return .different
    }
}



init(id : Int, identify : String, latitude : Double, longitude : Double){
    
    self.id = id
    self.identify = identify
    self.longitude = longitude
    self.latitude = latitude
}

}





 struct JsonData : Decodable {
let places : [Location]

}

Right here all my annotationsView :

 import MapKit



 class GlassAnnotationView : MKMarkerAnnotationView {

static let reuseID = "glassAnnotation"

override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
    tremendous.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been carried out")
}

override func prepareForDisplay() {
    tremendous.prepareForDisplay()
    displayPriority = .defaultHigh
    markerTintColor = UIColor.blue
    glyphImage = UIImage(systemName: "thermometer.solar.fill")

    

}

}


class GarbageAnnotationView : MKMarkerAnnotationView {

static let reuseID = "garbageAnnotation"

override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
    tremendous.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been carried out")
}

override func prepareForDisplay() {
    tremendous.prepareForDisplay()
    displayPriority = .defaultHigh
    markerTintColor = UIColor.purple
    glyphImage = UIImage(systemName: "thermometer.solar.fill")

}

}


 class SeparateAnnotationView : MKMarkerAnnotationView {

static let reuseID = "separateAnnotation"

override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
    tremendous.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been carried out")
}

override func prepareForDisplay() {
    tremendous.prepareForDisplay()
    displayPriority = .defaultHigh
    markerTintColor = UIColor.inexperienced
    glyphImage = UIImage(systemName: "dawn.fill")
}

}


class OtherAnnotationView : MKMarkerAnnotationView {

static let reuseID = "otherAnnotation"

override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
    tremendous.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been carried out")
}

override func prepareForDisplay() {
    tremendous.prepareForDisplay()
    displayPriority = .defaultHigh
    markerTintColor = UIColor.black
    glyphImage = UIImage(systemName: "photograph.circle.fill")

}

}

And my ViewController

  import CoreLocation
  import UIKit
  import MapKit

  class ViewController: UIViewController, CLLocationManagerDelegate {


var places = [Location]()

@IBOutlet weak var mapView: MKMapView!

let supervisor = CLLocationManager()



override func viewDidLoad() {
    
    tremendous.viewDidLoad()
    
    readFile()
  //        registerAnnotationViewClasses()


    
}


 


override func viewDidAppear(_ animated: Bool) {
    tremendous.viewDidAppear(animated)
    supervisor.desiredAccuracy = kCLLocationAccuracyBest
    supervisor.delegate = self
    supervisor.requestWhenInUseAuthorization()
    supervisor.startUpdatingLocation()
    mapView.showsUserLocation = true






}

func locationManager(_ supervisor: CLLocationManager, didUpdateLocations places:  [CLLocation]) {
    if let location = places.first {
        supervisor.stopUpdatingLocation()
        
        render(location)
    }
}

func render(_ location:CLLocation){
    let coordinate = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
    
    let span = MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)
    
    let area = MKCoordinateRegion(middle: coordinate,
                                    span: span)
    
    mapView.setRegion(area, animated: true)

}


non-public func registerAnnotationViewClasses() {
    mapView.register(GlassAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
    mapView.register(OtherAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
    mapView.register(ClusterAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultClusterAnnotationViewReuseIdentifier)
    mapView.register(SeparateAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
    mapView.register(GarbageAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)



}



public func readFile() {
    if let url = Bundle.important.url(forResource : "capital", withExtension: "json"),
       let information = attempt? Knowledge(contentsOf: url) {
        let decoder = JSONDecoder()
        if let jsonData = attempt? decoder.decode(JsonData.self, from: information){
            self.places = jsonData.places
            mapView.addAnnotations(jsonData.places)

        }
    }
}

  }

  extension ViewController: MKMapViewDelegate {

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    guard let annotation = annotation as? Location else { return nil }

    swap annotation.sort {
    case .rubbish:
        return GarbageAnnotationView(annotation: annotation, reuseIdentifier: GarbageAnnotationView.reuseID)
    case .separate:
        return SeparateAnnotationView(annotation: annotation, reuseIdentifier: SeparateAnnotationView.reuseID)
    case .different:
        return OtherAnnotationView(annotation: annotation, reuseIdentifier: OtherAnnotationView.reuseID)
    case .glass:
        return GlassAnnotationView(annotation: annotation, reuseIdentifier: GlassAnnotationView.reuseID)
    }
}

}