Is there a way to display data from an extension? - swiftui

I am having some trouble displaying data that is inside an extension.
Here is some sample data:
[Evvie.Course(AddressInfo: Evvie.AddressInfo(Title: "Commerce Street", Latitude: 30.78049027304715, Longitude: -91.37567342143757)),
Evvie.Course(AddressInfo: Evvie.AddressInfo(Title: "Electrify America (Target)", Latitude: 45.80593414847252, Longitude: -108.47424014718615)),
Evvie.Course(AddressInfo: Evvie.AddressInfo(Title: "29247 ", Latitude: 34.439655548047284, Longitude: -80.19629017657348))]
And here are how my things are defined:
struct Course: Hashable, Codable {
let AddressInfo: AddressInfo
}
struct AddressInfo: Hashable, Codable {
let Title: String
let Latitude: Double
let Longitude: Double
}
extension AddressInfo {
var coordinate:CLLocationCoordinate2D? {
let coordinate = CLLocationCoordinate2D(latitude: Latitude, longitude: Longitude)
return coordinate
}
}
My question is how would I be able to access something such as the coordinate? Something like this:
ForEach(viewModel.courses, id: \.self) { course in
Text(course.AddressInfo.coordinate)
}

Related

How I can clustering map annotation in swiftui?

I searched for many different ways but didn't find any with swiftui.
I tried to do it through MKMapView. But I have custom points and a lot of functionality is tied to swiftui.
import SwiftUI
import MapKit
struct ContentView: View {
#State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 43.64422936785126, longitude: 142.39329541313924),
span: MKCoordinateSpan(latitudeDelta: 1.5, longitudeDelta: 2)
)
var body: some View {
Map(coordinateRegion: $region, annotationItems: data) { annotation in
MapAnnotation(coordinate: annotation.coordinate) {
Image(systemName: "person.circle.fill")
.resizable()
.frame(width: 20, height: 20)
.foregroundColor(Color.purple)
}
}
.edgesIgnoringSafeArea(.all)
}
}
struct SampleData: Identifiable {
var id = UUID()
var latitude: Double
var longitude: Double
var coordinate: CLLocationCoordinate2D {
CLLocationCoordinate2D(
latitude: latitude,
longitude: longitude)
}
}
var data = [
SampleData(latitude: 43.70564024126748, longitude: 142.37968945214223),
SampleData(latitude: 43.81257464206404, longitude: 142.82112322464369),
SampleData(latitude: 43.38416585162576, longitude: 141.7252598737476),
SampleData(latitude: 45.29168643283501, longitude: 141.95286751470724),
SampleData(latitude: 45.49261392585982, longitude: 141.9343973160499),
SampleData(latitude: 44.69825427301145, longitude: 141.91227845284203)
]
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Please help find a solution. I will be glad to any advice.

SwiftUI/ MapKit - Populate a MapAnnotation Struct from MongoDB Realm/ Atlas collection

I am new to SwiftUI and Realm (using flexible sync), please excuse if it sounds like an elementary question
I have location data saved in a MongoDB Atlas collection - Centre
class Centre: Object, ObjectKeyIdentifiable {
#Persisted var _id: ObjectId = ObjectId.generate()
#Persisted var centreName = ""
#Persisted var centreDesc = ""
#Persisted var centreLocation: Coordinates?
override static func primaryKey() -> String? {
return "_id"
}
convenience init(centreName: String, centreDesc: String, centreLocation: Coordinates) {
self.init()
self.centreName = centreName
self.centreDesc = centreDesc
self.centreLocation = centreLocation
}
}
and Coordinates are embedded objects where "x" is longitude and "y" is latitude
class Coordinates: EmbeddedObject, ObjectKeyIdentifiable {
#Persisted var x: Double?
#Persisted var y: Double?
}
I have created a Struct to conform to the requirements of MapAnnotation protocol as -
struct CustomAnnots: Identifiable {
let id: UUID
var nameCentreLoc: String
var descCentreLoc: String
let latitude: Double
let longitude: Double
var coordinate: CLLocationCoordinate2D {
CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
}
}
I am trying to populate this struct from the data from Atlas collection
My LocationView - not working
import SwiftUI
import MapKit
import RealmSwift
struct LocationView: View {
#Environment(\.realm) var realm
#ObservedResults(Centre.self) var centres
#ObservedRealmObject var centre: Centre
#State private var nameCentreLoc = ""
#State private var descCentreLoc = ""
#State private var latitude = 0.0
#State private var longitude = 0.0
#State private var annots = []
#State private var region = MKCoordinateRegion(
center: CLLocationCoordinate2D(latitude: 24.681_858, longitude: 81.811_623),
span: MKCoordinateSpan(latitudeDelta: 10, longitudeDelta: 10)
)
var body: some View {
Map(coordinateRegion: $region, annotationItems: annots, annotationContent: { locations in
MapPin(coordinate: locations.coordinate)
})
.onAppear {
setSubscription()
initData()
}
}
private func setSubscription() {
let subscriptions = realm.subscriptions
subscriptions.write {
if let currentSubscription = subscriptions.first(named: "all_centres") {
currentSubscription.update(toType: Centre.self) { centre in
centre.centreName != ""
}
} else {
subscriptions.append(QuerySubscription<Centre>(name: "all_centres") { centre in
centre.centreName != ""
})
}
}
}
private func initData() {
nameCentreLoc = centre.centreName
descCentreLoc = centre.centreDesc
latitude = (centre.centreLocation?.y)!
longitude = (centre.centreLocation?.x)!
let annots = [for centre in centres {
CustomAnnots(id: UUID(), nameCentreLoc: nameCentreLoc, descCentreLoc: descCentreLoc, latitude: latitude, longitude: longitude)
}]
}
}
How do I populate the Struct with data from Centre collection?
Changed to (with no errors in Xcode)-
var body: some View {
let annots = [CustomAnnots(id: UUID(), nameCentreLoc: centre.centreName, descCentreLoc: centre.centreDesc, latitude: (centre.centreLocation?.y)!, longitude: (centre.centreLocation?.x)!)]
Map(coordinateRegion: $region, annotationItems: annots, annotationContent: { locations in
MapPin(coordinate: locations.coordinate)
})
.onAppear {
setSubscription()
}
}
now getting runtime error "Force unwrapping nil value"
with this function I am able to print out the results to console
func getLoc() {
for centre in centres {
var annots = [CustomAnnots.init(id: UUID(), nameCentreLoc: centre.centreName, descCentreLoc: centre.centreDesc, latitude: (centre.centreLocation?.y)!, longitude: (centre.centreLocation?.x)!)]
print(annots)
}
}
I get this printed out -
[ACCv5.CustomAnnots(id: 67E9DADA-0BCC-4D30-8136-8B666881E82D, nameCentreLoc: "HO", descCentreLoc: "Head Office Artemis Cardiac Care Gurgaon", latitude: 28.438694893842058, longitude: 77.10845294294181)]
[ACCv5.CustomAnnots(id: 26DC0C63-5A17-49C7-B4BF-FD3AA1ABF65E, nameCentreLoc: "Panipat", descCentreLoc: "Artemis Heart Centre at Ravindra Hospital", latitude: 29.388306713854682, longitude: 76.95889693063663)]
[ACCv5.CustomAnnots(id: D3A70E58-6B65-4F5D-A398-3394B7FB04DF, nameCentreLoc: "Ranchi", descCentreLoc: "Artemis Heart Centre at Raj Hospital", latitude: 23.35731237118492, longitude: 85.32288933068195)]
But I am unable to display MapAnnotations with this -
#Environment(\.realm) var realm
#ObservedResults(Centre.self) var centres
#State public var annots: [CustomAnnots]
#State private var region = MKCoordinateRegion(
center: CLLocationCoordinate2D(latitude: 24.681_858, longitude: 81.811_623),
span: MKCoordinateSpan(latitudeDelta: 10, longitudeDelta: 10)
)
var body: some View {
ForEach (centres) { centre in
var annots = [CustomAnnots.init(id: UUID(), nameCentreLoc: centre.centreName, descCentreLoc: centre.centreDesc, latitude: (centre.centreLocation?.y)!, longitude: (centre.centreLocation?.x)!)]
}
// Text("\(annots.count)")
Map(coordinateRegion: $region, annotationItems: annots, annotationContent: { locations in
MapMarker(coordinate: locations.coordinate)
})
Final code after Jay's suggestions
class Centre: Object, ObjectKeyIdentifiable {
#Persisted var _id: ObjectId = ObjectId.generate()
#Persisted var centreName = ""
#Persisted var centreDesc = ""
#Persisted var centreLocation: Coordinates?
override static func primaryKey() -> String? {
return "_id"
}
convenience init(centreName: String, centreDesc: String, x: Double, y: Double) {
self.init()
self.centreName = centreName
self.centreDesc = centreDesc
self.centreLocation?.x = x
self.centreLocation?.y = y
}
var coordinate: CLLocationCoordinate2D {
CLLocationCoordinate2D(latitude: (centreLocation?.y)!, longitude: (centreLocation?.x)!)
}
}
Map(coordinateRegion: $region, annotationItems: centres, annotationContent: { centre in
MapMarker(coordinate: centre.coordinate)
})
Simplifying may produce a more streamlined solution.
If the objective is to populate a map from Realm objects, then I think you're on the right track but there are too many moving parts - lets reduce that code. Let me know if I misunderstood the question..
Here's some pseudo code:
Start with the Realm object that contains the data. Note we don't need the primary key because we are using ObjectId's for that. This object has everything needed to track the pin on the map, it can be observed for changes and returns a calculated var containing the MapAnnotation to be used on the map itself, based on the data in the object
class Centre: Object, ObjectKeyIdentifiable {
#Persisted var _id: ObjectId = ObjectId.generate()
#Persisted var centreName = ""
#Persisted var centreDesc = ""
#Persisted var x: Double!
#Persisted var y: Double!
var mapAnno: MapAnnotation {
//build the map annotation from the above properties
return //return the map annotation
}
convenience init(centreName: String, centreDesc: String, x: Double, y: Double) {
self.init()
self.centreName = centreName
self.centreDesc = centreDesc
self.x = x
self.y = y
}
}
Then in the view - load up all of the centers and iterate over them retreiving the MapAnnotation from each
#ObservedResults(Centre.self) var centres //loads all of the centrs
var body: some View {
ForEach (centres) { centre in
let mapAnnotation = centre.mapAnno
//add the annotation to the map
Note this pattern is pretty common as well
Map(coordinateRegion: $region,
showsUserLocation: true,
annotationItems: centres) { center in
MapAnnotation(coordinate: //get coords from centre)

Argument passed to call that takes no arguments when trying to pin some locations on a MapView

I'm getting this error and can't figure out why.
Can you help me?
struct Locs: Identifiable {
let id = UUID()
let name = String()
let coord = CLLocationCoordinate2D()
let alarm = Int()
let state = Int()
let radius = Int()
}
struct MapView: View {
#State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 5.55613, longitude: 95.3218), span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
let annotations = [
Locs(name: "Sultan Hotel", coord: CLLocationCoordinate2D(latitude: 5.55739, longitude: 95.3208), alarm: 0, state: 0, radius: 1),
Locs(name: "Paparon Pizza Lhokseumawe", coord: CLLocationCoordinate2D(latitude: 5.17837, longitude: 97.1484), alarm: 0, state: 1, radius: 2)
]
You can't provide default values outside of the init, unless you want the properties to be declared as a var.
Either:
Don't have default values (unless it won't change and not needed for memberwise init)
struct Locs: Identifiable {
let id = UUID()
let name: String
let coord: CLLocationCoordinate2D
let alarm: Int
let state: Int
let radius: Int
}
Have default values with a custom init
struct Locs: Identifiable {
let id = UUID()
let name: String
let coord: CLLocationCoordinate2D
let alarm: Int
let state: Int
let radius: Int
init(name: String = "some default name", coord: CLLocationCoordinate2D, alarm: Int, state: Int, radius: Int) {
self.name = name
self.coord = coord
self.alarm = alarm
self.state = state
self.radius = radius
}
}

Clustering annotation with swiftUI

My goal is clustering annotation on map with show number of items in cluster, I have no experience in UIKit and try to avoid it. Is it possible to do it using swiftUI only? If not how to reduce intervention of UIKit?
This is how it should look like
import SwiftUI
import MapKit
struct ContentView: View {
#State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 43.64422936785126, longitude: 142.39329541313924),
span: MKCoordinateSpan(latitudeDelta: 1.5, longitudeDelta: 2)
)
var body: some View {
Map(coordinateRegion: $region, annotationItems: data) { annotation in
MapAnnotation(coordinate: annotation.coordinate) {
Image(systemName: "person.circle.fill")
.resizable()
.frame(width: 20, height: 20)
.foregroundColor(Color.purple)
}
}
.edgesIgnoringSafeArea(.all)
}
}
struct SampleData: Identifiable {
var id = UUID()
var latitude: Double
var longitude: Double
var coordinate: CLLocationCoordinate2D {
CLLocationCoordinate2D(
latitude: latitude,
longitude: longitude)
}
}
var data = [
SampleData(latitude: 43.70564024126748, longitude: 142.37968945214223),
SampleData(latitude: 43.81257464206404, longitude: 142.82112322464369),
SampleData(latitude: 43.38416585162576, longitude: 141.7252598737476),
SampleData(latitude: 45.29168643283501, longitude: 141.95286751470724),
SampleData(latitude: 45.49261392585982, longitude: 141.9343973160499),
SampleData(latitude: 44.69825427301145, longitude: 141.91227845284203)
]
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
I find the way to cluster annotations with MapKit, but reuse map like a view for easy swiftUI. Looks like that https://i.stack.imgur.com/u3hKR.jpg
import SwiftUI
import MapKit
struct MapView: UIViewRepresentable {
var forDisplay = data
#State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 43.64422936785126, longitude: 142.39329541313924),
span: MKCoordinateSpan(latitudeDelta: 1.5, longitudeDelta: 2)
)
class Coordinator: NSObject, MKMapViewDelegate {
var parent: MapView
init(_ parent: MapView) {
self.parent = parent
}
/// showing annotation on the map
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard let annotation = annotation as? LandmarkAnnotation else { return nil }
return AnnotationView(annotation: annotation, reuseIdentifier: AnnotationView.ReuseID)
}
}
func makeCoordinator() -> Coordinator {
MapView.Coordinator(self)
}
func makeUIView(context: Context) -> MKMapView {
/// creating a map
let view = MKMapView()
/// connecting delegate with the map
view.delegate = context.coordinator
view.setRegion(region, animated: false)
view.mapType = .standard
for points in forDisplay {
let annotation = LandmarkAnnotation(coordinate: points.coordinate)
view.addAnnotation(annotation)
}
return view
}
func updateUIView(_ uiView: MKMapView, context: Context) {
}
}
struct SampleData: Identifiable {
var id = UUID()
var latitude: Double
var longitude: Double
var coordinate: CLLocationCoordinate2D {
CLLocationCoordinate2D(
latitude: latitude,
longitude: longitude)
}
}
var data = [
SampleData(latitude: 43.70564024126748, longitude: 142.37968945214223),
SampleData(latitude: 43.81257464206404, longitude: 142.82112322464369),
SampleData(latitude: 43.38416585162576, longitude: 141.7252598737476),
SampleData(latitude: 45.29168643283501, longitude: 141.95286751470724),
SampleData(latitude: 45.49261392585982, longitude: 141.9343973160499),
SampleData(latitude: 44.69825427301145, longitude: 141.91227845284203)
]
class LandmarkAnnotation: NSObject, MKAnnotation {
let coordinate: CLLocationCoordinate2D
init(
coordinate: CLLocationCoordinate2D
) {
self.coordinate = coordinate
super.init()
}
}
/// here posible to customize annotation view
let clusterID = "clustering"
class AnnotationView: MKMarkerAnnotationView {
static let ReuseID = "cultureAnnotation"
/// setting the key for clustering annotations
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
clusteringIdentifier = clusterID
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func prepareForDisplay() {
super.prepareForDisplay()
displayPriority = .defaultLow
}
}
And use that map like a default view
import SwiftUI
struct ContentView: View {
var body: some View {
MapView()
.edgesIgnoringSafeArea(.all)
}
}
For solving a problem I used next resources:
https://www.hackingwithswift.com/books/ios-swiftui/communicating-with-a-mapkit-coordinator
https://www.hackingwithswift.com/books/ios-swiftui/advanced-mkmapview-with-swiftui
https://developer.apple.com/videos/play/wwdc2017/237/
https://www.youtube.com/watch?v=QuYA7gQjTt4

Transferring Coordinates to a MapView with Pin

I am trying to display a map pin on a map. Upon entry of a transaction the details are saved along with the location coordinates. In a list of transaction entries, the user may click on an entry for more detail information including a small map showing the transaction location.
Based on Asperi's suggestions at adding a MapMarker to MapKit in swiftUI 2 it appears that I need to declare an identifiable structure in order to use a map pin.
In the DetailView the latitude and longitude are copied to a coordinate parameter before transmission to MapView.
struct DetailView: View {
var coordinate: CLLocationCoordinate2D {
CLLocationCoordinate2D(
latitude: item.entryLat,
longitude: item.entryLong)
}
var body: some View {
VStack {
MapView(coordinate: coordinate)
.ignoresSafeArea(edges: .all)
.frame(height: 400)
.padding(.vertical, 10)
}
}
}
MapView is where I'm having trouble. I'm not sure how to pass in my coordinates for the region and the marker (xxxxx). Copying` coordinate to the #State region and the marker produces the error "Argument passed to call that takes no arguments".
struct Marker: Identifiable {
let id = UUID()
var location: MapMarker
}
struct MapView: View {
var coordinate: CLLocationCoordinate2D
#State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(xxxxxxx), span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5))
let markers = [Marker(location: MapMarker(coordinate: CLLocationCoordinate2D(xxxxxxx), tint: .red))]
var body: some View {
Map(coordinateRegion: $region, showsUserLocation: true,
annotationItems: markers) { marker in
marker.location
}.edgesIgnoringSafeArea(.all)
}
}
Sounds like for your application, declaring the region as a constant will work. The code would look like this:
struct Marker: Identifiable {
let id = UUID()
var location: MapMarker
}
struct MapView: View {
var coordinate: CLLocationCoordinate2D
var body: some View {
Map(coordinateRegion: .constant(MKCoordinateRegion(center: coordinate,
span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5))),
showsUserLocation: true,
annotationItems: [Marker(location: MapMarker(coordinate: coordinate))]) { marker in
marker.location
}.edgesIgnoringSafeArea(.all)
}
}
If you still wanted to use it as a #State variable, you could use a custom init to set the value:
struct MapView: View {
var coordinate: CLLocationCoordinate2D
#State private var region : MKCoordinateRegion
init(coordinate : CLLocationCoordinate2D) {
self.coordinate = coordinate
_region = State(initialValue: MKCoordinateRegion(center: coordinate,
span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5)))
}
var body: some View {
Map(coordinateRegion: $region,
showsUserLocation: true,
annotationItems: [Marker(location: MapMarker(coordinate: coordinate))]) { marker in
marker.location
}
.edgesIgnoringSafeArea(.all)
}
}
Lastly, I'm defining the markers array inline, but you could split it out into a computed property:
var markers : [Marker] {
[Marker(location: MapMarker(coordinate: coordinate))]
}
var body: some View {
Map(coordinateRegion: $region,
showsUserLocation: true,
annotationItems: markers) { marker in
marker.location
}
.edgesIgnoringSafeArea(.all)
}