Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 2173
Alex Hales
  • 0
Alex HalesTeacher
Asked: May 31, 20222022-05-31T17:07:59+00:00 2022-05-31T17:07:59+00:00

ios – Xcode and Swift: Colors and Object ID not being applied to map markers?

  • 0

[ad_1]

I’m making a test app that is taking in geological points of interest from a JSON file and plotting the points on a map (you can see the info I am getting here: https://maine.hub.arcgis.com/datasets/ff3e487fb782464684f8c1f8a1b7e58d_0/about and you can see the JSON file there as well). I’ve been trying to color-code the points to correspond to the category of geological feature (bedrock is red, coastal is green, surficial is purple, etc.). However, when I try to apply the colors, it doesn’t work. The app runs just fine, and I can see all the points, they’re just not color-coded or labeled as their object ID.

This is the file I used for each item (titled Artwork because this project was previously locations of artworks in Oahu, but changed to geological formations in Maine):

import Foundation
import MapKit
import Contacts
import SwiftUI

class Artwork: NSObject, MKAnnotation {
    let title: String?
    let locationName: String?
    let type: String?
    let coordinate: CLLocationCoordinate2D
    let objectID: Int?
    
    init(title: String?, locationName: String?, type: String?, coordinate: CLLocationCoordinate2D, objectID: Int?) {
        self.title = title
        self.locationName = locationName
        self.type = type
        self.coordinate = coordinate
        self.objectID = objectID
        
        super.init()
    }
    
    init?(feature: MKGeoJSONFeature) {
        guard
            let point = feature.geometry.first as? MKPointAnnotation,
            let propertiesData = feature.properties,
            let json = try? JSONSerialization.jsonObject(with: propertiesData),
            let properties = json as? [String: Any]
        else {
            return nil
        }
        
        title = properties["SITE_NAME"] as? String
        locationName = properties["TOWN"] as? String
        type = properties["CATEGORY"] as? String
        coordinate = point.coordinate
        objectID = properties["OBJECTID"] as? Int
        super.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.name = title
        return mapItem
    }
    
    //This is where I code the method for choosing the color
    var markerTintColor: UIColor {
        switch type {
        case "Bedrock":
            return .red
        case "Coastal":
            return .green
        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 .gray
        }
    }
}

Here is another file that might be important, ArtworkViews.swift:

import Foundation
import MapKit
import SwiftUI

class ArtworkViews: MKMarkerAnnotationView {
    override var annotation: MKAnnotation? {
        willSet {
            guard let artwork = newValue as? Artwork else {
                return
            }
            canShowCallout = true
            calloutOffset = CGPoint(x: -5, y: 5)
            rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
            
            //This is where the color and ID are applied
            markerTintColor = artwork.markerTintColor
            if let ID = artwork.objectID {
                glyphText = String(ID)
            }
        }
    }
}

And here is where I implement the colors in ViewController.swift (this the viewDidLoad() function):

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

//This is where the color is ultimately applied        
mapView.register(ArtworkViews.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
        
loadInitialData()
mapView.addAnnotations(artworks)

[ad_2]

  • 0 0 Answers
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 5256 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 1098 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 1047 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.