iosarraysswiftmkmapviewcllocationcoordinate2d

Change CLLocationCoordinate2DMake With array by indexPath


Im trying to code the CLLocationCoordinate2DMake to change by an array so the lat and long will change depending on the indxPath ... like i tried here:

var Longitude = ["32.101145","32.074961","",""]
var Latitude = ["34.775163","34.781679","","",""]


//What i tried :

    let LightHouseLocation = CLLocationCoordinate2DMake(Longitude[indexPath.row],Latitude[indexPath.row])
            // Drop a pin

but of course its throwing errors on me . i will be grateful if anyone could help me, Thank you .


Solution

  • This is how I would do it.

    class ViewController: UIViewController {
    
        @IBOutlet weak var map: MKMapView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
    
            var coordinates = Array<CLLocationCoordinate2D>()
    
            coordinates.append(CLLocationCoordinate2DMake(32.101145, 32.074961))
    
            coordinates.append(CLLocationCoordinate2DMake(34.775163, 34.781679))
    
            let pins = coordinates.map { (coordinate) -> MKPointAnnotation in
    
                let pin = MKPointAnnotation()
    
                pin.coordinate = coordinate
    
                return pin
            }
    
            self.map.addAnnotations(pins)
        }
    }