我如何从结构中获取纬度和经度并将它们显示在 mapbox Swift
how do i get the latitude and longitude from struct and show them on mapbox Swift
我正在从 API 获取数据,我需要一个纬度和经度数组来显示地图上的所有位置(我在地图上使用 Mapbox)
func getdata(){
print("meter is \(mapMeter)")
var semaphore = DispatchSemaphore (value: 0)
let parameters = "{\n \"latitude\" : \"41.76484\",\n\"longitude\" : \"123.3968636\",\n\"dist\": \"\(mapMeter)\"\n}"
let postData = parameters.data(using: .utf8)
var request = URLRequest(url: URL(string: "http://app.freewayfinder.com/api/locations")!,timeoutInterval: Double.infinity)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
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))
semaphore.signal()
return
}
let getlocation = try? JSONDecoder().decode(Response.self, from: data)
guard let locations = getlocation else {return}
self.locations = locations.message
self.collectionView.reloadData()
semaphore.signal()
}
task.resume()
semaphore.wait()
}
位置结构:
struct Response : Decodable {
let success : Bool
let code : Int
let message : [location]
}
struct location : Decodable{
let id : Int
let name : String
let img : String
let latitude : String
let longitude : String
let city : String
let country : String
let created_at : String
let updated_at : String
}
请帮我解决这个问题我想在地图上显示分配
您可以像这样从地图中的数组设置位置
for locationStruct in self.locations
{
let annotation = MGLPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: Double(locationStruct.latitude), longitude: Double(locationStruct.longitude))
annotation.title = locationStruct.name
annotation.subtitle = "Your Subtitle"
self.mapView.addAnnotation(annotation)
}
我正在从 API 获取数据,我需要一个纬度和经度数组来显示地图上的所有位置(我在地图上使用 Mapbox)
func getdata(){
print("meter is \(mapMeter)")
var semaphore = DispatchSemaphore (value: 0)
let parameters = "{\n \"latitude\" : \"41.76484\",\n\"longitude\" : \"123.3968636\",\n\"dist\": \"\(mapMeter)\"\n}"
let postData = parameters.data(using: .utf8)
var request = URLRequest(url: URL(string: "http://app.freewayfinder.com/api/locations")!,timeoutInterval: Double.infinity)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
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))
semaphore.signal()
return
}
let getlocation = try? JSONDecoder().decode(Response.self, from: data)
guard let locations = getlocation else {return}
self.locations = locations.message
self.collectionView.reloadData()
semaphore.signal()
}
task.resume()
semaphore.wait()
}
位置结构:
struct Response : Decodable {
let success : Bool
let code : Int
let message : [location]
}
struct location : Decodable{
let id : Int
let name : String
let img : String
let latitude : String
let longitude : String
let city : String
let country : String
let created_at : String
let updated_at : String
}
请帮我解决这个问题我想在地图上显示分配
您可以像这样从地图中的数组设置位置
for locationStruct in self.locations
{
let annotation = MGLPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: Double(locationStruct.latitude), longitude: Double(locationStruct.longitude))
annotation.title = locationStruct.name
annotation.subtitle = "Your Subtitle"
self.mapView.addAnnotation(annotation)
}