Hi guys
I have the following problem:
I have MapKit view in swiftui where I'm updating a boolean variable so I know when all the data points are displayed.
I have created the following class and I can "see" the variable changing from false to true and vice versa.
class GettingData: ObservableObject {
@Published var doneGettingData : Bool = false
{
didSet {
if doneGettingData {
print("The instance property doneGettingData is now true.")
} else {
print("The instance property doneGettingData is now false.")
}
}
}
}
The problem is how to use this variable in ContentView so I can turn on and off a spinner.
I have tried several approaches but nothing works.
In MapView I'm doing it the following way
struct MapView: UIViewRepresentable {
let shared = GettingData()
var startdate : String
var depth: String
// The makeUIView(context:) method is creates an empty Map View.
func makeUIView(context: Context) -> MKMapView{
MKMapView(frame: .zero)
}
func makeCoordinator() -> MapViewCoordinator{
MapViewCoordinator(self)
}
func updateUIView(_ uiView: MKMapView, context: Context){
self.shared.doneGettingData = false
\\\\ rest of the code
uiView.addAnnotations(allLocations)
self.shared.doneGettingData = true
\\\\ rest of the code
The variable does change when I have everything in the map
But how to use it for the spinner ?
Any ideias please ?
Thank you
Post
Replies
Boosts
Views
Activity
Hi everyone. This might be a silly question, but I'm new at swift.
I have the following piece of code that I call mapView that loads a map with data from a url json .
import SwiftUI
import MapKit
import Combine
var locationsFillTest : Int = 0
var allLocations = [MKPointAnnotation]()
var doneGettingData : Bool = false {
didSet {
if doneGettingData {
print("The instance property doneGettingData is now true.")
} else {
print("The instance property doneGettingData is now false.")
}
}
}
struct MapView: UIViewRepresentable {
var startdate : String
var depth: String
func makeUIView(context: Context) -> MKMapView{
MKMapView(frame: .zero)
}
func makeCoordinator() -> MapViewCoordinator{
MapViewCoordinator(self)
}
func updateUIView(_ uiView: MKMapView, context: Context){
uiView.removeAnnotations(allLocations)
allLocations = []
doneGettingData = false
let url = URL(string: "https://xxxx.com")!
URLSession.shared.dataTask(with: url) {(data,response,error) in
do {
if let d = data {
let decodedLists = try JSONDecoder().decode(emsc.self, from: d)
DispatchQueue.main.async {
locationsFillTest = allLocations.count
for locations in decodedLists.features {
var mapRegion = MKCoordinateRegion()
let mapRegionSpan = 24.0
mapRegion.span.latitudeDelta = mapRegionSpan
mapRegion.span.longitudeDelta = mapRegionSpan
let lat = Double(locations.properties.lat)
let long = Double(locations.properties.lon
let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: lat , longitude: long )
uiView.addAnnotations(allLocations)
uiView.delegate = context.coordinator
uiView.showAnnotations(uiView.annotations, animated: true)
doneGettingData = true
}
} catch {
print("Error decoding JSON: ", error, response!)
}
}.resume()
}
}
struct MapView_Previews: PreviewProvider {
static var previews: some View {
MapView(startdate: "2020-08-23",depth: "All")
}
}
so far so good the points get to the map, and the variable doneGettingData does change from false too true when all the points are loaded.
But now I would like to use this variable to set a spinner in my contentView, the issue is that although the variable its available and I can see it changing in the print statement it does not change in the contentView, I have tested with a Text so I could checked it.
Any ideias please ?
Thank you
Hi allI developing an App using swiftui and mapkit that adds several layers to the map : Annotations, Circles and Polyline.I'm adding the polylines the folloiwng way:annotation.coordinate = CLLocationCoordinate2D(latitude: locations.lat, longitude: locations.lon)
let contagem = locations_map.count
let polyline = MKGeodesicPolyline(coordinates: locations_map, count: contagem )
//print(contagem)
let circle = MKCircle(center: destination, radius: locations.accuracy)
uiView.setRegion(mapRegion, animated: true)
uiView.delegate = context.coordinator
uiView.addOverlay(polyline)
uiView.addAnnotation(annotation)
uiView.addOverlay(circle)
Than when I need to remove the annotations the ciecles and the polylines I'm doing the folloiwng uiView.removeAnnotations(uiView.annotations)
uiView.removeOverlays(uiView.overlays)But all works except the polylines that persist in the map !!