Friday, October 23, 2026
HomeiOS Developmentios - Xcode and Swift: Colours and Object ID not being utilized...

ios – Xcode and Swift: Colours and Object ID not being utilized to map markers?

[ad_1]

I am making a take a look at app that’s taking in geological factors of curiosity from a JSON file and plotting the factors on a map (you possibly can see the information I get right here: https://maine.hub.arcgis.com/datasets/ff3e487fb782464684f8c1f8a1b7e58d_0/about and you may see the JSON file there as properly). I have been making an attempt to color-code the factors to correspond to the class of geological function (bedrock is purple, coastal is inexperienced, surficial is purple, and many others.). Nevertheless, when I attempt to apply the colours, it does not work. The app runs simply tremendous, and I can see all of the factors, they’re simply not color-coded or labeled as their object ID.

That is the file I used for every merchandise (titled Paintings as a result of this undertaking was beforehand places of artworks in Oahu, however modified to geological formations in Maine):

import Basis
import MapKit
import Contacts
import SwiftUI

class Paintings: NSObject, MKAnnotation {
    let title: String?
    let locationName: String?
    let sort: String?
    let coordinate: CLLocationCoordinate2D
    let objectID: Int?
    
    init(title: String?, locationName: String?, sort: String?, coordinate: CLLocationCoordinate2D, objectID: Int?) {
        self.title = title
        self.locationName = locationName
        self.sort = sort
        self.coordinate = coordinate
        self.objectID = objectID
        
        tremendous.init()
    }
    
    init?(function: MKGeoJSONFeature) {
        guard
            let level = function.geometry.first as? MKPointAnnotation,
            let propertiesData = function.properties,
            let json = strive? JSONSerialization.jsonObject(with: propertiesData),
            let properties = json as? [String: Any]
        else {
            return nil
        }
        
        title = properties["SITE_NAME"] as? String
        locationName = properties["TOWN"] as? String
        sort = properties["CATEGORY"] as? String
        coordinate = level.coordinate
        objectID = properties["OBJECTID"] as? Int
        tremendous.init()
    }
    
    var subtitle: String? {
        return locationName
    }
    
    var mapItem: MKMapItem? {
        guard let location = locationName else {
            return nil
        }
        
        let addressDict = [CNPostalAddressStreetKey: location]
        let placemark = MKPlacemark(coordinate: coordinate, addressDictionary: addressDict)
        let mapItem = MKMapItem(placemark: placemark)
        mapItem.identify = title
        return mapItem
    }
    
    //That is the place I code the strategy for selecting the colour
    var markerTintColor: UIColor {
        swap sort {
        case "Bedrock":
            return .purple
        case "Coastal":
            return .inexperienced
        case "Surficial":
            return .purple
        case "Bedrock, Surficial":
            return .blue
        case "Bedrock, Surficial, Coastal":
            return .cyan
        case "Surficial, Coastal":
            return .magenta
        case "Bedrock, Coastal":
            return .orange
        default:
            return .grey
        }
    }
}

Right here is one other file that is perhaps vital, ArtworkViews.swift:

import Basis
import MapKit
import SwiftUI

class ArtworkViews: MKMarkerAnnotationView {
    override var annotation: MKAnnotation? {
        willSet {
            guard let paintings = newValue as? Paintings else {
                return
            }
            canShowCallout = true
            calloutOffset = CGPoint(x: -5, y: 5)
            rightCalloutAccessoryView = UIButton(sort: .detailDisclosure)
            
            //That is the place the colour and ID are utilized
            markerTintColor = paintings.markerTintColor
            if let ID = paintings.objectID {
                glyphText = String(ID)
            }
        }
    }
}

And right here is the place I implement the colours in ViewController.swift (this the viewDidLoad() perform):

let initialLocation = CLLocation(latitude: 44.883427, longitude: -68.670815)
        
mapView.centerToLocation(initialLocation)
        
let maineCenter = CLLocation(latitude: 44.883427, longitude: -68.670815)
let area = MKCoordinateRegion(heart: maineCenter.coordinate, latitudinalMeters: 600000, longitudinalMeters: 300000)
mapView.setCameraBoundary(MKMapView.CameraBoundary(coordinateRegion: area), animated: true)
        
let zoomRange = MKMapView.CameraZoomRange(maxCenterCoordinateDistance: 1400000)
mapView.setCameraZoomRange(zoomRange, animated: true)
        
mapView.delegate = self

//That is the place the colour is finally utilized        
mapView.register(ArtworkViews.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
        
loadInitialData()
mapView.addAnnotations(artworks)

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments