i am a junior in react native i am working on building an app with gps mapbox sdk inside of it. I've made a bridge that working well but i would like to send user coordinates in parallel every 30 seconds(during the navigation), but i didn't find a way to do it
here is my swift (working ) code that launche the mapbox navigation with a simple request
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)
}
}
}
Thank you
the thing to do is to run each method separately in the main thread asynchronously and a the timer where we want
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)
}