我的地图不显示来自 JSON 文件的注释 (UIKit / Swift /MapKit)
My map doesn't show annotations from my JSON file (UIKit / Swift /MapKit)
我是 Swift 的法国新手,我想使用 UIKit 和 MapKit 从 JSON 文件中制作带有注释的地图。我使用 UIKit 是因为我想使用很多注释,并且我希望如果用户缩小,注释会折叠成一个群集注释。
因此,项目已构建,但显示了我的 none 个注释
非常感谢任何纠正我的代码的人。如果您有任何疑问,请随时问我。提前谢谢你。
所以我的 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
}
]
}
'''
和我的位置 class :
'''
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]
}
'''
经过一些研究,我发现了我的问题:
- :mapView.addAnnotations 需要使用 jsonData.locations 而不是位置
'''
mapView.addAnnotations(jsonData.locations)
'''
- 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)
}
}
}
'''
- 最后我只需要在 viewDidAppear 中调用我的函数:
'''
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.delegate = self
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
mapView.showsUserLocation = true
readFile()
}
'''
我是 Swift 的法国新手,我想使用 UIKit 和 MapKit 从 JSON 文件中制作带有注释的地图。我使用 UIKit 是因为我想使用很多注释,并且我希望如果用户缩小,注释会折叠成一个群集注释。
因此,项目已构建,但显示了我的 none 个注释
非常感谢任何纠正我的代码的人。如果您有任何疑问,请随时问我。提前谢谢你。
所以我的 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
}
]
}
'''
和我的位置 class :
'''
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]
}
'''
经过一些研究,我发现了我的问题:
- :mapView.addAnnotations 需要使用 jsonData.locations 而不是位置
'''
mapView.addAnnotations(jsonData.locations)
'''
- 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)
}
}
}
'''
- 最后我只需要在 viewDidAppear 中调用我的函数:
'''
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.delegate = self
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
mapView.showsUserLocation = true
readFile()
}
'''