【发布时间】:2021-04-15 06:01:19
【问题描述】:
我是 swift 的新手,我用静态数据实现了一个 MapKit,它运行良好,我调用了后端引脚数据,它在操场上显示它运行良好,但地图没有显示标记,似乎 mapKit 没有在正确的时间捕获引脚数据,所以我使用 Dispatch.Que 刷新地图但我没有刷新并且它显示没有标记
这是我尝试过的:
import UIKit
import MapKit
class myMapViewController: UIViewController, MKMapViewDelegate {
var shops = [Shops]()
var communities = [Community]()
var cyclists = [Cyclist]()
var circuits = [Circuit]()
var BR = BaseUrl.baseUrl
@IBOutlet weak var myMap: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
self.getShops()
self.getCircuits()
self.getCyclists()
self.getCommunities()
//shops.append(Shops(id: 0, title: "Shop1", latitude: 36.553015 , longitude: 10.592774))
//shops.append(Shops(id: 0, title: "Shop2", latitude: 35.499414 , longitude: 10.824846))
//communities.append(Community(id: 0, title: "community1", latitude: 37.276943 , longitude: 10.934709 ))
//communities.append(Community(id: 0, title: "community2", latitude: 35.427828 , longitude: 9.748186 ))
//circuits.append(Circuit(id: 0, title: "circuit1", latitude: 33.773035 , longitude: 10.857805 ))
//cyclists.append(Cyclist(id: 0, title: "cyclist1", latitude: 35.785118 , longitude: 10.000871 ))
createShopsAnnotations(locations: shops)
createCircuitsAnnotations(locations: circuits)
createCommunityAnnotations(locations: communities)
createCyclistsAnnotations(locations: cyclists)
}
func createShopsAnnotations(locations:[Shops]){
for location in locations {
let annotations = MKPointAnnotation()
annotations.title = location.title as? String
annotations.coordinate = CLLocationCoordinate2D(latitude: location.latitude as! CLLocationDegrees , longitude: location.longitude as! CLLocationDegrees)
DispatchQueue.main.async {
self.myMap.addAnnotation(annotations)
}
}}
func createCircuitsAnnotations(locations:[Circuit]){
for location in locations {
let annotations = MKPointAnnotation()
annotations.title = location.title as? String
annotations.coordinate = CLLocationCoordinate2D(latitude: location.latitude as! CLLocationDegrees , longitude: location.longitude as! CLLocationDegrees)
DispatchQueue.main.async {
self.myMap.addAnnotation(annotations)
}
}
}
func createCommunityAnnotations(locations:[Community]){
for location in locations {
let annotations = MKPointAnnotation()
annotations.title = location.title as? String
annotations.coordinate = CLLocationCoordinate2D(latitude: location.latitude as! CLLocationDegrees , longitude: location.longitude as! CLLocationDegrees)
DispatchQueue.main.async {
self.myMap.addAnnotation(annotations)
}
}}
func createCyclistsAnnotations(locations:[Cyclist]){
for location in locations {
let annotations = MKPointAnnotation()
annotations.title = location.title as? String
annotations.coordinate = CLLocationCoordinate2D(latitude: location.latitude as! CLLocationDegrees , longitude: location.longitude as! CLLocationDegrees)
DispatchQueue.main.async {
self.myMap.addAnnotation(annotations)
}
}}
func getShops(){
//get
guard let url = URL(string: BR+"/shops") else {
return
}
let session = URLSession.shared
session.dataTask(with: url) { ( data , response ,error) in
if let response = response {
print(response)
}
if let data = data {
print(data)
do
{
let json = try JSONSerialization.jsonObject(with: data, options: [])as! [[String:Any]]
self.shops.removeAll()
for item in json {
let id = item["shop_id"] as! Int
let title = item["title"] as! String
let latitude = item["latitude"] as! Double
let longitude = item["longitude"] as! Double
self.shops.append(Shops(id: id, title: title, latitude: latitude , longitude: longitude))
}
for item in self.shops {
print(item.shop_id)
print(item.title)
print(item.latitude)
print(item.longitude)
}
}catch{
print(error)
}
}
}.resume()
}
func getCommunities(){
//get
guard let url = URL(string: BR+"/communities") else {
return
}
let session = URLSession.shared
session.dataTask(with: url) { ( data , response ,error) in
if let response = response {
print(response)
}
if let data = data {
print(data)
do
{
let json = try JSONSerialization.jsonObject(with: data, options: [])as! [[String:Any]]
self.communities.removeAll()
for item in json {
let id = item["community_id"] as! Int
let title = item["title"] as! String
let latitude = item["latitude"] as! Double
let longitude = item["longitude"] as! Double
self.communities.append(Community(id: id, title: title, latitude: latitude , longitude: longitude))
}
for item in self.communities {
print(item.community_id)
print(item.title)
print(item.latitude)
print(item.longitude)
}
}catch{
print(error)
}
}
}.resume()
}
func getCircuits(){
//get
guard let url = URL(string: BR+"/circuits") else {
return
}
let session = URLSession.shared
session.dataTask(with: url) { ( data , response ,error) in
if let response = response {
print(response)
}
if let data = data {
print(data)
do
{
let json = try JSONSerialization.jsonObject(with: data, options: [])as! [[String:Any]]
self.shops.removeAll()
for item in json {
let id = item["circuit_id"] as! Int
let title = item["title"] as! String
let latitude = item["latitude"] as! Double
let longitude = item["longitude"] as! Double
self.circuits.append(Circuit(id: id, title: title, latitude: latitude , longitude: longitude))
}
for item in self.circuits {
print(item.circuit_id)
print(item.title)
print(item.latitude)
print(item.longitude)
}
}catch{
print(error)
}
}
}.resume()
}
func getCyclists(){
//get
guard let url = URL(string: BR+"/cyclists") else {
return
}
let session = URLSession.shared
session.dataTask(with: url) { ( data , response ,error) in
if let response = response {
print(response)
}
if let data = data {
print(data)
do
{
let json = try JSONSerialization.jsonObject(with: data, options: [])as! [[String:Any]]
self.cyclists.removeAll()
for item in json {
let id = item["cyclist_id"] as! Int
let title = item["title"] as! String
let latitude = item["latitude"] as! Double
let longitude = item["longitude"] as! Double
self.cyclists.append(Cyclist(id: id, title: title, latitude: latitude , longitude: longitude))
}
for item in self.cyclists {
print(item.cyclist_id)
print(item.title)
print(item.latitude)
print(item.longitude)
}
}catch{
print(error)
}
}
}.resume()
}
}
我要做的是让 mapkit 在正确的时间捕获引脚数据并刷新它的数据,我认为这是正确显示我的引脚的唯一方法
【问题讨论】:
-
get 方法是异步的,这意味着在调用“create”方法时它们可能还没有准备好。使用 create 方法作为回调调用“setMethods”。
-
这里是一个带有回调的请求示例:link.medium.com/ruslwMJOUcb
-
那么 DispatchQueue.main.async { myMap.delegate = self }
-
你应该在 viewDidLoad 中设置的委托,它已经是主队列。不要在主队列中添加每个添加:矫枉过正。将整个创建回调包装在主队列中
-
我正在寻找一个简单的技巧来解决这个问题,不幸的是使用回调很难,还有什么更简单的吗?看来这对初学者来说太难了
标签: swift xcode mapkit urlsession