0

I am developing a Google Maps app and I have the following code:

func drawPolygon(){
var cord: Array<CLLocationCoordinate2D> = []
let path = GMSMutablePath()
// get path for polygon
for marker in markersArray {
let lat = coord.insert(marker.componentsSeparatedByString(",")[i] as NSString).doubleValue, atIndex:[i][0]
let lng = coord.insert(marker.componentsSeparatedByString(",")[i+1] as NSString).doubleValue, atIndex:[i][1]
path.addCoordinate(CLLocationCoordinate2DMake(coord[i][0]))
path.addCoordinate(CLLocationCoordinate2DMake(coord[i][1]))
}

As you can see I would like to increment the index by one every time so all coordinates get inserted. How do I achieve this?

2 Answers 2

2

There is a build in mechanism for that.

func drawPolygon(){
var cord: Array<CLLocationCoordinate2D> = []
let path = GMSMutablePath()
// get path for polygon
for (marker, i) in markersArray.enumerated() {
let lat = coord.insert(marker.componentsSeparatedByString(",")[i] as NSString).doubleValue, atIndex:[i][0]
let lng = coord.insert(marker.componentsSeparatedByString(",")[i+1] as NSString).doubleValue, atIndex:[i][1]
path.addCoordinate(CLLocationCoordinate2DMake(coord[i][0]))
path.addCoordinate(CLLocationCoordinate2DMake(coord[i][1]))
}
Sign up to request clarification or add additional context in comments.

Comments

1
    func drawPolygon(){
    var cord: Array<CLLocationCoordinate2D> = []
    let path = GMSMutablePath()
    // get path for polygon
//initailizing i = 0;
    var i = 0
    for marker in markersArray {
    let lat = coord.insert(marker.componentsSeparatedByString(",")[i] as NSString).doubleValue, atIndex:[i][0]
    let lng = coord.insert(marker.componentsSeparatedByString(",")[i+1] as NSString).doubleValue, atIndex:[i][1]
    path.addCoordinate(CLLocationCoordinate2DMake(coord[i][0]))
    path.addCoordinate(CLLocationCoordinate2DMake(coord[i][1]))
//incrementing i with +1
    i = i+1;
    }
    }

1 Comment

Thank you so much!

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.