4

I have a dynamic amount of locations being plotted onto a mapkit. I am curious on how I can get my current latitudes and longitudes into a single array, as they are currently being printed as separate objects which is not plotting not the map like it should. I know the problem but not sure how to fix it. Here is my current code to produce the coordinates -

  do {
        let place = try myContext.executeFetchRequest(fetchRequest) as! [Places]

        for coords in place{
            let latarray = Double(coords.latitude!)
            let lonarray = Double(coords.longitude!)
            let arraytitles = coords.title!

            let destination:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latarray, lonarray)

        print(destination)

    } catch let error as NSError {
        // failure
        print("Fetch failed: \(error.localizedDescription)")
    }

And here is the print in the console - Output

What I need the print to look like to work correctly - Desired output

I hope you understand what I mean. I am very grateful of any help! Thank you for reading.

rscode101
  • 55
  • 1
  • 4
  • please show more of your code than that one line, which creates a single coordinate, not an array of any sort. – luk2302 Feb 08 '16 at 19:04
  • @luk2302: It doesn't create a single coordinate, as you can see with the output image that holds user inputted places in core data as coordinates. I have updated the post with more code. – rscode101 Feb 08 '16 at 19:13

1 Answers1

5

You could create an array of CLLocationCoordinate2Ds:

var coordinateArray: [CLLocationCoordinate2D] = []

if latarray.count == lonarray.count {
    for var i = 0; i < latarray.count; i++ {
        let destination = CLLocationCoordinate2DMake(latarray[i], lonarray[i])
        coordinateArray.append(destination)
    }
}

EDIT:

In your code, neither latarray nor lonarray is an array. If you want to create an array of CLLocationCoordinate2Ds, you should add a variable to store your locations and your for loop should look like this:

var locations: [CLLocationCoordinate2D] = []

for coords in place{
    let lat = Double(coords.latitude!)
    let lon = Double(coords.longitude!)
    let title = coords.title!

    let destination = CLLocationCoordinate2DMake(lat, lon)
    print(destination) // This prints each location separately

    if !locations.contains(destination) {
        locations.append(destination)
    }
}

print(locations) // This prints all locations as an array

// Now you can use your locations anywhere in the scope where you defined the array.
func getLocationFromArray() {
    // Loop through the locations array:
    for location in locations {
        print(location) // Prints each location separately again
    }
}
xoudini
  • 7,001
  • 5
  • 23
  • 37
  • The problem I have now is that it is created an array each time for the amount of places are in core data. This image shows [link](http://i.imgur.com/Luc3Qkh.png) this. There is currently 4 places stored, which the last array shows in the output. How can I get around this? – rscode101 Feb 08 '16 at 19:33
  • Silly mistake, i put the print outside the loop and all is displayed as correctly! Thank you! I still have issues with line plotting, but I can move forward now. Thanks again. – rscode101 Feb 08 '16 at 19:42
  • @rscode101 No problems, just remember to mark as answered when you're satisfied with an answer to your question on SO. – xoudini Feb 08 '16 at 19:44
  • I have come across another issue that maybe you can help me with. When creating the MKPointAnnotion how can I use the stored array coordinates we just created, as I get this error **Cannot assign value of type '[CLLocationCoordinate2D]' to type 'CLLocationCoordinate2D'** Any ideas? @dzk – rscode101 Feb 08 '16 at 20:17
  • The `locations` is now an array, so you need to choose one object from it to assign to each `MKPointAnnotation`, I'll update the answer above. – xoudini Feb 08 '16 at 20:26