我有一台运行node.js的服务器,该服务器的URL上包含一组地理数据。我想从iOS应用程序向其发送POST请求,并用点填充我的应用程序地图。数据的结构如下:
{
'lat': 13.37,
'long': 42.00,
'name': 'epic geographic datapoint'
}
我想说我已经尝试过一些东西,但是我在这里完全迷路了。我是iOS开发的新手,但这是我的代码:
class ViewController: UIViewController, CLLocationManagerDelegate {
let manager = CLLocationManager()
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[0]
let userCurrentLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
let region = MKCoordinateRegion.init(center: userCurrentLocation, latitudinalMeters: 100, longitudinalMeters: 100);
map.setRegion(region, animated: true)
}
//MAP
@IBOutlet var map: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization();
manager.startUpdatingLocation();
// Do any additional setup after loading the view.
}
//POST an endpoint and populate the MKMapView
}```
Thanks
使用URLSession
,请求看起来像这样:
import Foundation
var semaphore = DispatchSemaphore (value: 0)
let parameters = "{\n \"lat\": 13.37,\n \"long\": 42,\n \"name\": \"epic geographic datapoint\"\n}"
let postData = parameters.data(using: .utf8)
var request = URLRequest(url: URL(string: "https://insertURL.com")!,timeoutInterval: Double.infinity)
request.httpMethod = "POST"
request.httpBody = postData
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data else {
print(String(describing: error))
return
}
// for this example print the response
print(String(data: data, encoding: .utf8)!)
semaphore.signal()
}
task.resume()
semaphore.wait()