gpt4 book ai didi

ios - 在用户在 map 上拖动之前,来自 json 的 swift 4 注释不可见

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:59:24 26 4
gpt4 key购买 nike

我在显示 map 上的注释时遇到问题。它们仅在我移动或拖动 map 时显示。

我尝试按照 youtube 上的教程进行操作,其中一个问题是 c sharp

请有人帮忙......这是代码

这里我为注解创建了一个类

  class customPin: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var title: String?
var subtitle: String?

init(pinTitle:String, pinSubTitle:String, location:CLLocationCoordinate2D) {
self.title = pinTitle
self.subtitle = pinSubTitle
self.coordinate = location
}}

class ViewController: UIViewController, CLLocationManagerDelegate {

@IBOutlet weak var mapView: MKMapView!

var locationManager = CLLocationManager()

override func viewDidLoad() {
super.viewDidLoad()

mapView.register(CustomAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)

self.locationManager.requestWhenInUseAuthorization()


//json show the list of the parks


pipiCanList()

//show location Barcelona
let location = CLLocationCoordinate2D(latitude: 41.3851 , longitude:2.1734)
let region = MKCoordinateRegion(center: location, span: MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05))

self.mapView.setRegion(region, animated: true)
self.mapView.delegate = self

get the json information

 func pipiCanList(){

let url = "http://bluewave.lk/Apps/pipiCan/Api/read.php";
let urlObj = URL(string:url);

URLSession.shared.dataTask(with: urlObj!) {(data, response, error) in
do{
let pipiCans = try JSONDecoder().decode([pipiCanDBList].self, from: data!)

for pipiCan in pipiCans{

let Latitude = (pipiCan.ParkLatitude! as NSString).doubleValue
let Longitude = (pipiCan.ParkLongitude! as NSString).doubleValue
let location = CLLocationCoordinate2D(latitude: Latitude, longitude: Longitude)

//add to
let pin = customPin(pinTitle: pipiCan.ParkName!, pinSubTitle: pipiCan.Area!, location: location)
self.mapView.addAnnotation(pin)

}
} catch{

print ("Error - cannot get list")

}
}.resume()


}
}

最佳答案

你需要

DispatchQueue.main.async { 
let pin = customPin(pinTitle: pipiCan.ParkName!, pinSubTitle: pipiCan.Area!, location: location)
self.mapView.addAnnotation(pin)
}

由于 URLSession.shared.dataTask 回调在后台线程中,您应该在主线程中访问 UIKit 元素,例如 mapView 您可以也做

self.mapView.showAnnotations(self.mapView.annotations, animated: true)

在 for 循环之后显示所有添加的注释也请记住你在这里设置一个区域

 let location = CLLocationCoordinate2D(latitude: 41.3851   , longitude:2.1734)
let region = MKCoordinateRegion(center: location, span: MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05))

响应可能具有比上述区域更远的位置值

关于ios - 在用户在 map 上拖动之前,来自 json 的 swift 4 注释不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55347525/

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