Что я сделал: Карта с несколькими аннотациями Работает и выглядит очень хорошо, но
Screen-Picture from Daniel Frank-Vetter
What I like to have
... I like to have the Geo-data in the json-file within the project-navigator (Bundle).
I was able to make a clusterAnnotationView with the .plist.
Please be so kind to give me a few lines of code, how to do it with SwiftUI and .json in
bundle.
I searched GitHub, stackoverflow and tried out several days without success.
1. This is the Code-Data for the Annotations
import Foundation
import MapKit
import SwiftUI
class **LandmarkAnnotation** : NSObject, MKAnnotation {
let title: String?
let subtitle: String?
let coordinate: CLLocationCoordinate2D
init(title: String?,
subtitle: String?,
coordinate: CLLocationCoordinate2D)
{self.title = title
self.subtitle = subtitle
self.coordinate = coordinate}
static func requestMockData()-> [LandmarkAnnotation]{
return [
LandmarkAnnotation(title: "Aachen",
subtitle: "Karls Dom",
coordinate: .init(latitude: 50.774910, longitude: 6.083951)),
LandmarkAnnotation(title: "Vaals",
subtitle: "Aachens `kleine Schwerster` in den Niederlanden",
coordinate: .init(latitude: 50.773437, longitude: 6.016159)),
LandmarkAnnotation(title: "Köln",
subtitle: "Dom",
coordinate: .init(latitude: 50.941278, longitude: 6.958281))
2. This is the code in MapView
import SwiftUI
import MapKit
struct **MapView**: UIViewRepresentable {
var coordinate: CLLocationCoordinate2D
let landmarks = LandmarkAnnotation.requestMockData()
func makeUIView(context: Context) -> MKMapView {
MKMapView(frame: .zero)
}
func updateUIView(_ view: MKMapView, context: Context) {
let span = MKCoordinateSpan(latitudeDelta: 0.02, longitudeDelta: 0.02)
let region = MKCoordinateRegion(center: coordinate, span: span)
view.setRegion(region, animated: true)
**view.addAnnotations(landmarks)**
}
}
3. This is the data.json File
[{
"name": "Dreiländer-Eck",
"category": "A",
"city": "Vaals, Kelmis, Aachen",
"state": "NE, BE, D ",
"id": 1001,
"park": "Weltkulturerbe",
"coordinates": {
"longitude": 6.020776,
"latitude": 50.754482
},
"imageName": "dreilaenderpunkt"
},
{
"name": "Dom zu Köln",
"category": "B",
"city": "Köln",
"state": "NRW",
"id": 1002,
"park": "Weltkulturerbe",
"coordinates": {
"longitude": 6.95828,
"latitude": 50.94145
},
"imageName": "dom"
}]
4. This is the Model
struct **Landmark**: Hashable, Codable, Identifiable {
var id: Int
var name: String
fileprivate var imageName: String
fileprivate var coordinates: Coordinates
var state: String
var park: String
var category: Category
var locationCoordinate: CLLocationCoordinate2D {
CLLocationCoordinate2D(
latitude: coordinates.latitude,
longitude: coordinates.longitude)
}
enum Category: String, CaseIterable, Codable, Hashable {
case featured = "A"
case lakes = "B"
case rivers = "C"
}
}
extension Landmark {
var image: Image {
ImageStore.shared.image(name: imageName)
}
}
struct Coordinates: Hashable, Codable {
var latitude: Double
var longitude: Double
}
5. Here the code for parsing landmarkData.json from Model
let landmarkData: [Landmark] = load("landmarkData.json")
func load(_ filename: String) -> T {
let data: Data
Ссылка: Учебник Apple SwiftUI