I’m having a problem loading the map. Here’s a gif showing the problem, it’s once in a while, not every time. :
Normally, the case that should work is as shown in the image below :
Here is the View code and the ViewModel code :
<code>struct Map4View: View {
@StateObject var mapViewModel:Map4ViewModel = Map4ViewModel()
var body: some View {
Map() {
ForEach(mapViewModel.pointsMission.filter { pointsMission in pointsMission.isCurrent}) { currentPoint in
Annotation("(currentPoint.latitude), (currentPoint.longitude)", coordinate: CLLocationCoordinate2D(latitude: currentPoint.latitude, longitude: currentPoint.longitude)) {
ZStack {
RoundedRectangle(cornerRadius: 5).fill(.white)
RoundedRectangle(cornerRadius: 5).stroke(Color.init(hex: "8aa873"))
Image(systemName: "ant.fill").padding(5).foregroundStyle(Color.init(hex: "8aa873"))
}
}
}
if let route = mapViewModel.currentRoute{
MapPolyline(route.polyline)
.stroke(Color.blue, lineWidth: 4)
}
}
.mapStyle(.hybrid(elevation: .realistic)).edgesIgnoringSafeArea(.all)
.safeAreaInset(edge: .bottom) {
HStack {
Spacer()
Button(action: { mapViewModel.validateMosquitoRelease() }) {
Text("Valider la libération des moustiques")
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(8)
}.disabled(mapViewModel.isButtonVisible).padding()
Spacer()
}.background(.white.opacity(0.8))
}.onAppear {
Task { await mapViewModel.getPointsMission() }
}
}
}
</code>
<code>struct Map4View: View {
@StateObject var mapViewModel:Map4ViewModel = Map4ViewModel()
var body: some View {
Map() {
ForEach(mapViewModel.pointsMission.filter { pointsMission in pointsMission.isCurrent}) { currentPoint in
Annotation("(currentPoint.latitude), (currentPoint.longitude)", coordinate: CLLocationCoordinate2D(latitude: currentPoint.latitude, longitude: currentPoint.longitude)) {
ZStack {
RoundedRectangle(cornerRadius: 5).fill(.white)
RoundedRectangle(cornerRadius: 5).stroke(Color.init(hex: "8aa873"))
Image(systemName: "ant.fill").padding(5).foregroundStyle(Color.init(hex: "8aa873"))
}
}
}
if let route = mapViewModel.currentRoute{
MapPolyline(route.polyline)
.stroke(Color.blue, lineWidth: 4)
}
}
.mapStyle(.hybrid(elevation: .realistic)).edgesIgnoringSafeArea(.all)
.safeAreaInset(edge: .bottom) {
HStack {
Spacer()
Button(action: { mapViewModel.validateMosquitoRelease() }) {
Text("Valider la libération des moustiques")
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(8)
}.disabled(mapViewModel.isButtonVisible).padding()
Spacer()
}.background(.white.opacity(0.8))
}.onAppear {
Task { await mapViewModel.getPointsMission() }
}
}
}
</code>
struct Map4View: View {
@StateObject var mapViewModel:Map4ViewModel = Map4ViewModel()
var body: some View {
Map() {
ForEach(mapViewModel.pointsMission.filter { pointsMission in pointsMission.isCurrent}) { currentPoint in
Annotation("(currentPoint.latitude), (currentPoint.longitude)", coordinate: CLLocationCoordinate2D(latitude: currentPoint.latitude, longitude: currentPoint.longitude)) {
ZStack {
RoundedRectangle(cornerRadius: 5).fill(.white)
RoundedRectangle(cornerRadius: 5).stroke(Color.init(hex: "8aa873"))
Image(systemName: "ant.fill").padding(5).foregroundStyle(Color.init(hex: "8aa873"))
}
}
}
if let route = mapViewModel.currentRoute{
MapPolyline(route.polyline)
.stroke(Color.blue, lineWidth: 4)
}
}
.mapStyle(.hybrid(elevation: .realistic)).edgesIgnoringSafeArea(.all)
.safeAreaInset(edge: .bottom) {
HStack {
Spacer()
Button(action: { mapViewModel.validateMosquitoRelease() }) {
Text("Valider la libération des moustiques")
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(8)
}.disabled(mapViewModel.isButtonVisible).padding()
Spacer()
}.background(.white.opacity(0.8))
}.onAppear {
Task { await mapViewModel.getPointsMission() }
}
}
}
<code> func getPointsMission() async {
pointsMission = [
PointsMission(latitude: 45.8342019, longitude: 1.2537218 ),
PointsMission(latitude: 45.8343859, longitude: 1.2521066 ),
PointsMission(latitude: 45.8356658, longitude: 1.2526660 ),
etc...
]
pointsMission[currentIndex].isCurrent = true;
createRoute(
start: locationManager.location?.coordinate,
end: CLLocationCoordinate2D(latitude: pointsMission[currentIndex].latitude, longitude: pointsMission[currentIndex].longitude)
)
}
</code>
<code> func getPointsMission() async {
pointsMission = [
PointsMission(latitude: 45.8342019, longitude: 1.2537218 ),
PointsMission(latitude: 45.8343859, longitude: 1.2521066 ),
PointsMission(latitude: 45.8356658, longitude: 1.2526660 ),
etc...
]
pointsMission[currentIndex].isCurrent = true;
createRoute(
start: locationManager.location?.coordinate,
end: CLLocationCoordinate2D(latitude: pointsMission[currentIndex].latitude, longitude: pointsMission[currentIndex].longitude)
)
}
</code>
func getPointsMission() async {
pointsMission = [
PointsMission(latitude: 45.8342019, longitude: 1.2537218 ),
PointsMission(latitude: 45.8343859, longitude: 1.2521066 ),
PointsMission(latitude: 45.8356658, longitude: 1.2526660 ),
etc...
]
pointsMission[currentIndex].isCurrent = true;
createRoute(
start: locationManager.location?.coordinate,
end: CLLocationCoordinate2D(latitude: pointsMission[currentIndex].latitude, longitude: pointsMission[currentIndex].longitude)
)
}
If you can help me, I’m already 2 days into my search… Thank you 😉