Problem with MapBox PointAnnotations with data from Alamofire

92 views Asked by At

In my app I need to get annotations coordinates from server, so im getting is with Alamofire request. And when I try to add annotations to map from Alamofire pid im getting unknown error

class MapViewController : UIViewController, MGLMapViewDelegate, FloatingPanelControllerDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        ....
        ....
        getDataFromServer()
    }
    
    func getDataFromServer() {
        let coffeeShopsJSON = AF.request("http://194.6*******")
        coffeeShopsJSON.responseJSON { response in
            switch response.result {
                case .success(let value):
                    let jsonFile = JSON(value)
            
                    let arrayCount = jsonFile.array?.count ?? 0
                    var id = [Int]()
                    var name = [String]()
                    var lat = [Double]()
                    var lon = [Double]()
                    var img = [UIImage(named : "cofShopIcon"), UIImage(named : "cofShopIcon"), UIImage(named : "cofShopIcon"), UIImage(named : "cofShopIcon"), UIImage(named : "cofShopIcon")] //test images
                    
                    for i in 0..<arrayCount {
                        let j_id = jsonFile[i]["id"].string ?? "-1"
                        id.append(Int(j_id) ?? -1)
                        
                        let j_name = jsonFile[i]["name"].string ?? "N/A"
                        name.append(j_name)
                        
                        let j_lat = jsonFile[i]["coordX"].string ?? "-1.0"
                        lat.append(Double(j_lat) ?? -1.0)
                        
                        let j_lon = jsonFile[i]["coordY"].string ?? "-1.0"
                        lon.append(Double(j_lon) ?? -1.0)
                    }
                    
                    for i in 0..<id.count {
                        mapData.append(coffeeShopsData(shopId : id[i], name : name[i], icon : img[i], latitude : lat[i], longitude : lon[i]))
                    }
                    
                    for i in 0..<mapData.count {
                        let point = MGLPointAnnotation()
                        point.coordinate = CLLocationCoordinate2D(latitude : mapData[i].latitude ?? 0, longitude : mapData[i].longitude ?? 0)
                        point.title = mapData[i].name ?? "*Data Error*"
                        markers.append(point)
                    }
                    self.mapView.addAnnotations(markers)
                                       
                 case .failure(let error):
                    print(error)
            }
        }
    }

}

Error: libc++abi.dylib: terminating with uncaught exception of type std::domain_error

I'm new in swift, so any advice will be useful

0

There are 0 answers