在 ios 的 mapbox 导航期间执行 post 请求

execute post request during mapbox navigation in ios

我是react native的大三学生 我正在构建一个内置 gps mapbox sdk 的应用程序。 我做了一座运行良好的桥梁,但我想每 30 秒(在导航期间)并行发送一次用户坐标,但我没有找到实现它的方法

这是我的swift(有效)代码,它通过一个简单的请求启动 mapbox 导航


import Foundation;
import UIKit
import MapboxDirections
import MapboxCoreNavigation
import MapboxNavigation

@objc(AndroidRN) class AndroidRN: NSObject {

  @objc
  func takeMeToWH(number:Double,number2:Double,number3:Double,number4:Double,number5:String,number6:String)->Void {


    let url = URL(string: "https://api.get-move.pro/v1/mobile/api/location")
    guard let requestUrl = url else { fatalError() }

    let body:[String:Any] = ["mission_id":number5,"depart_lat":number2,"depart_lon":number,"destination_lon":number3,"destination_lat":number4]
    let finalBody = try! JSONSerialization.data(withJSONObject: body)
    var request = URLRequest(url: requestUrl)

    request.httpMethod = "POST";
    request.httpBody = finalBody;

    request.setValue("application/json", forHTTPHeaderField: "Accept")
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    request.setValue("Authorization", forHTTPHeaderField: number6)


    let task = URLSession.shared.dataTask(with: request) { (data, response, error) in

            if let error = error {
                print("Error took place \(error)")
                return
            }

            if let data = data, let dataString = String(data: data, encoding: .utf8) {
                print("Response data string:\n \(dataString)")
            }
    }
    task.resume()

    let origin = Waypoint(coordinate: CLLocationCoordinate2D(latitude: number2, longitude: number), name: "Mapbox")
    let destination = Waypoint(coordinate: CLLocationCoordinate2D(latitude: number4, longitude: number3), name: "White House")
    let options = NavigationRouteOptions(waypoints: [origin, destination])
    Directions.shared.calculate(options) { (waypoints, routes, error) in
    guard let route = routes?.first else { return }
    let viewController = NavigationViewController(for: route)
    let appDelegate = UIApplication.shared.delegate
    appDelegate!.window!!.rootViewController!.present(viewController, animated: true, completion: nil)
    }
  }
}

谢谢

要做的事情就是运行每个方法在主线程中单独异步 和我们想要的计时器

 DispatchQueue.main.async{

      if #available(iOS 10.0, *) {
        Timer.scheduledTimer(withTimeInterval: 5, repeats: true) { timer in
          let url = URL(string: "https://api.get-move.pro/v1/mobile/api/location")
          guard let requestUrl = url else { fatalError() }

          //rest of code


    }
    DispatchQueue.main.async {
        launchNavigation(number:number,number2:number2,number3:number3,number4:number4)

    }