gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:27:23 25 4
gpt4 key购买 nike

我是 React Native 的大三学生我正在努力构建一个应用程序,其中包含 GPS Mapbox sdk。我已经制作了一个运行良好的桥,但我想每 30 秒并行发送一次用户坐标(在导航期间),但我没有找到方法来做到这一点

这是我的快速(工作)代码,它通过一个简单的请求启动 map 框导航


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)

}

关于swift - 在ios中的mapbox导航期间执行post请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59861475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com