0

Hello everyone i would like to get data from 3 arrays for creating map annotations. But i could not load data from those 3 arrays to a class. Here is my code:

My class file:

import MapKit
class Jobdata: NSObject, MKAnnotation {
    let title: String?
    let coordinate: CLLocationCoordinate2D
    init(title: String,  coordinate: CLLocationCoordinate2D) {
        self.title = title
        self.coordinate = coordinate
        super.init()
    }
}

These are my arrays:

var jobNameST = [String]()
var jobLongitudeST = [Double]()
var jobLatitudeST = [Double]()

And this is my locations array:

let jobLocations = [Jobdata(title: "test", coordinate: CLLocationCoordinate2D(latitude: 30.4692991035765, longitude:  -97.7660876))]

I would like to add those 3 arrays to my locations array.

1 Answer 1

1

First you need to create location model I Hope all arrays have same number of records Please check before if not

struct Location 
{
    var title: String?
    var latitude: Double?
    var longitude: Double?

    init(title: String, latitude: Double, longitude: Double) {
        self.title = title
        self.latitude = latitude
        self.longitude = longitude
    }
}

create location model array from given title, latitude and longitude arrays

    func getLocations() -> [Location] {

            var locations = [Location]()
            for (index, value) in jobNameST.enumerated() {
                let location = Location(title: value, latitude: jobLatitudeST[index], longitude: jobLongitudeST[index])
                locations.append(location)
            }
            return locations
   }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.