Swift - 如何从 mapViewkit 中的 API 数据创建要使用的局部变量

Posted

技术标签:

【中文标题】Swift - 如何从 mapViewkit 中的 API 数据创建要使用的局部变量【英文标题】:Swift - How to create local variables to use from the API data in mapViewkit 【发布时间】:2021-11-23 12:20:41 【问题描述】:

我想用来自 API 的数据替换纬度和经度,我只是不知道如何从 MapViewController 调用它们,所有设置都只是如何从我创建并连接的结构中调用它们APICaller.swift

Vehicles.swift 结构:

struct Vehicles: Codable 
    var IDVehicle: Int?
    var Title: String?
    var RegistrationDate: String?
    var ExpireDate: String?
    var Department: String?
    var Identification: String?
    var Speed: String?
    var Latitude: Double?
    var Longitude: Double?
    var Angle: Int?
    var Status: Int?
    var InputValue: Int?
    var Plate: String?
    var LastCommunicationDate: String?
    var Passengers: Int?
    var Driver: String?

现在我需要把纬度和经度放在这里“另一个视图控制器”:

let appleHQ = CLLocation(latitude: 37.334722 , longitude: 37.334722)
let regionRadius: CLLocationDistance = 1000.0
let region = MKCoordinateRegion(center: appleHQ.coordinate, latitudinalMeters:        regionRadius, longitudinalMeters: regionRadius)
mapView.setRegion(region, animated: true)
mapView.delegate = self

【问题讨论】:

【参考方案1】:
class APIManager
    
    static var sharedInstance:APIManager = APIManager()
    
    func apiCallerToFetchData(completion:(Result<Vehicles,Error>)->())
        //NetworkCall
        //After Getting Response assign that value ,but below i put as static code
        let vechicle = Vehicles(IDVehicle: 1, Title: "", RegistrationDate: "", ExpireDate: "", Department: "", Identification: "", Speed: "", Angle: 1, Status: 1, InputValue: 1, Plate: "", LastCommunicationDate: "", Passengers: 1)
        completion(.success(vechicle))
    


class MapViewController: UIViewController 
  var globalVechicle = Vehicles()
  var localLattitude :Double?
  var localLongitude :Double? 

  override func viewDidLoad() 
        super.viewDidLoad()
        fetchDataFromAPI _ in
            updateUIHere()
        
    
    
    func updateUIHere()
//you can either using globalVechicle.lattitude or localLattitude while assigning the value
        guard let lattitude = globalVechicle.Latitude ,let longitude = globalVechicle.Longitude else  return 
        let appleHQ = CLLocation(latitude: lattitude  , longitude: longitude)
        let regionRadius: CLLocationDistance = 1000.0
        let region = MKCoordinateRegion(center: appleHQ.coordinate, latitudinalMeters:        regionRadius, longitudinalMeters: regionRadius)
        mapView.setRegion(region, animated: true)
        mapView.delegate = self
    
    
    func fetchDataFromAPI(completion:(Bool)->())
        APIManager.sharedInstance.apiCallerToFetchData  result in
            switch result
            case .success(let vechicle):
                globalVechicle = vechicle
               localLattitude = vechicle.Latitude
               localLongitude = vechicle.Longitude

                completion(true)
            case .failure(let Error)
                completion(false)

            
        
    

【讨论】:

@L1S ,这就是你想要的方式 请检查以下代码 嘿伙计,代码正在运行,只是没有将确切位置显示为 API?

以上是关于Swift - 如何从 mapViewkit 中的 API 数据创建要使用的局部变量的主要内容,如果未能解决你的问题,请参考以下文章

如何从 iOS 中的流中接收数据(swift)

如何从 Swift 中的主线程中关闭

如何从 Swift 4 中的描述中解析数组?

如何从 Swift 3 中的数据中读取属性列表

如何从目标 c 中的 pod 文件访问我的 swift 项目文件?

如何从 Swift 中的 NSURLResponse 中检索 cookie?