首页 > 解决方案 > 我的地图没有显示来自我的 JSON 文件的注释(UIKit / Swift /MapKit)

问题描述

我是 Swift 的法国新手,我想使用 UIKit 和 MapKit 从 JSON 文件中创建一个带有注释的地图。我使用 UIKit 是因为我想使用很多注释,并且如果用户缩小,我希望注释折叠成一个集群注释。

因此,项目已构建,但我的注释均未显示

非常感谢任何纠正我的代码的人。如果您有任何问题,请随时问我。提前谢谢你。

所以这里是我的 ViewController :

'''

 import CoreLocation
 import UIKit
 import MapKit

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {


var locations = [Location]()

@IBOutlet weak var mapView: MKMapView!

let manager = CLLocationManager()



override func viewDidLoad() {
    
    super.viewDidLoad()
    mapView.delegate = self



    
    mapView.register(LocationDataMapClusterView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultClusterAnnotationViewReuseIdentifier)

    
}
func AnnotationView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    switch annotation {
    case is MKClusterAnnotation:
        return mapView.dequeueReusableAnnotationView(withIdentifier: MKMapViewDefaultClusterAnnotationViewReuseIdentifier, for: annotation)
    default:
        return nil
    }
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    manager.desiredAccuracy = kCLLocationAccuracyBest
    manager.delegate = self
    manager.requestWhenInUseAuthorization()
    manager.startUpdatingLocation()
    mapView.showsUserLocation = true
    readFile()
    mapView.addAnnotations(locations)

}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    if let location = locations.first {
        manager.stopUpdatingLocation()
        
        render(location)
    }
}

func render(_ location:CLLocation){
    let coordinate = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
    
    let span = MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)
    
    let region = MKCoordinateRegion(center: coordinate,
                                    span: span)
    
    mapView.setRegion(region, animated: true)
}

public func readFile() {
    if let url = Bundle.main.url(forResource : "capital", withExtension: "json"),
       let data = try? Data(contentsOf: url) {
        let decoder = JSONDecoder()
        if let jsonData = try? decoder.decode(JsonData.self, from: data){
            self.locations = jsonData.locations
            print(locations)
        }
    }
}



}

'''

我的 JSON 文件:

'''

{
"locations": [
    {
      "id": 0,
      "name": "New York City",
      "latitude": 40.71,
      "longitude": -74
    },
    {
      "id": 1,
      "name": "Barcelona",
      "latitude": 41.38,
      "longitude": 2.17
    },
    {
      "id": 2,
      "name": "Tokyo",
      "latitude": 35.68,
      "longitude": 139.76
    },
    {
      "id": 3,
      "name": "Atlanta",
      "latitude": 33.74,
      "longitude": -84.38
    },
    {
      "id": 4,
      "name": "Paris",
      "latitude": 48.85,
      "longitude": 2.35
    },
    {
      "id": 5,
      "name": "Hong Kong",
      "latitude": 22.31,
      "longitude": 114.16
    }
  ]
}

'''

和我的位置类:

'''

 import MapKit
 import UIKit

  class Location: NSObject, Decodable, Identifiable, MKAnnotation {


var id: Int
var name: String
var latitude: Double
var longitude : Double
var coordinate: CLLocationCoordinate2D {
    .init(latitude: latitude, longitude: longitude)
    
}




init(id : Int, name : String, latitude : Double, longitude : Double){
    
    self.id = id
    self.name = name
    self.longitude = longitude
    self.latitude = latitude
}

}




   struct JsonData : Decodable {
       let locations : [Location]
    }

'''

标签: swiftuikitmapkit

解决方案


经过一番研究,我发现了我的问题:

  1. : mapView.addAnnotations 需要使用 jsonData.locations 而不是位置

'''

mapView.addAnnotations(jsonData.locations)  

'''

  1. mapView.addAnnotations 必须像这样包含在 readfile 函数中:

'''

public func readFile() {
    if let url = Bundle.main.url(forResource : "capital", withExtension: "json"),
       let data = try? Data(contentsOf: url) {
        let decoder = JSONDecoder()
        if let jsonData = try? decoder.decode(JsonData.self, from: data){
            self.locations = jsonData.locations
            mapView.addAnnotations(jsonData.locations)
        }
    }
}

'''

  1. 最后我只需要在 viewDidAppear 中调用我的函数:

'''

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    manager.desiredAccuracy = kCLLocationAccuracyBest
    manager.delegate = self
    manager.requestWhenInUseAuthorization()
    manager.startUpdatingLocation()
    mapView.showsUserLocation = true
    readFile()
    
} 

'''


推荐阅读