OGeek|极客世界-中国程序员成长平台

标题: ios - swift 4 来自 json 的注释在用户拖动 map 之前不可见 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 22:39
标题: ios - swift 4 来自 json 的注释在用户拖动 map 之前不可见

我在 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()


    }
}    



Best Answer-推荐答案


你需要

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






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4