1

I have an array of location coordinates that are separated by : (-33.1231231:143.12312312) for example.

I am looping over the array to determine how many polygon points I need to create on a Google Map (CLLocationCoordinate2DMake)

for (index, element) in enumerate(userCoordinates) {
    println("\(element)")
}

I would like to split each element in to 2 parts at the : to create longitude and latitude values and then use these in as the coordinates.

I can't figure out how to split the element in to 2 pieces.

1
  • What is the type of element ? Is it a String ? Commented Jun 18, 2015 at 6:51

1 Answer 1

2

If your element is a String then you can separate do the following :

for (index, element) in enumerate(userCoordinates) {

    // If the parenthesis are present you can remove them:
    var stringElmt = element.stringByReplacingOccurrencesOfString("(", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)

    var elmtArray = split(stringElmt) {$0 == ":"}

    if elmtArray.count == 2 {
        let latitude = elmtArray[0]
        let longitude = elmtArray[1]

        // Do something with latitude and longitude
    }
}

The first line separate all the elements in your string with : being the separator and pass the different element in an array that you can then access by subscript.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.