Although the Markers are doing what I intended the actual Map View does not move to the new Marker location. The View remains on the first one viewed. This I don’t understand considering the Markers are updating. I DO APOLOGISE FOR THE FORMAT. TO FIT IT ALL IN I HAD TO REMOVE AS MUCH WHITESPACE AS POSSIBLE. I hope despite this someone can spot where I have gone wrong because despite days of staring at this I can’t!
Location Helper
let location = LocationHelper.currentLocation
extension LocationHelper: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { }
public func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Location manager failed with error: \(error.localizedDescription)")
}
public func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
print("Location manager changed the status: \(status)")
}
}
class LocationHelper: NSObject, ObservableObject {
static let shared = LocationHelper()
static let defaultLocation = CLLocationCoordinate2D(latitude: thisCardPositionLatitude, longitude: thisCardPositionLongitude)
static var currentLocation: CLLocationCoordinate2D {
guard let location = shared.locationManager.location else {
return defaultLocation
}
return location.coordinate
}
private let locationManager = CLLocationManager()
private override init() {
super.init()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
}
Map View
import SwiftUI
struct MyAnnotationItem: Identifiable {
var coordinate: CLLocationCoordinate2D
var color: Color?
let id = UUID()
}
struct GameMapView: View {
// Some code below deleted due to Character constraints
@ObservedObject var fourTeamBlue = FourTeamBlue()
// Some code above deleted due to Character constraints
@State private var seeNextCard = false
@State private var region: MKCoordinateRegion = MKCoordinateRegion(
center: CLLocationCoordinate2D(latitude: MapDefaults.latitude, longitude: MapDefaults.longitude), span: MKCoordinateSpan(latitudeDelta: MapDefaults.zoom, longitudeDelta: MapDefaults.zoom))
let annotationItems = [MyAnnotationItem(coordinate: CLLocationCoordinate2D(latitude: thisCardPositionLatitude, longitude: thisCardPositionLongitude), color: .red)]
private enum MapDefaults {
static let latitude = thisCardPositionLatitude
static let longitude = thisCardPositionLongitude
static let zoom = 0.0001
}
var body: some View {
VStack {
Map(coordinateRegion: $region, interactionModes: .all, showsUserLocation: true, annotationItems: annotationItems) { item in
MapMarker(coordinate: item.coordinate)
}
.edgesIgnoringSafeArea(.all)
}
VStack(alignment: .center){
Button(action: {
self.seeNextCard.toggle()
}){
Text("Next ▶️")
.font(.headline)
.foregroundColor(.black)
.frame(maxWidth: .infinity, alignment: .center)
.padding()
}.sheet(isPresented: $seeNextCard) {
// Some code below deleted due to Character constraints
if nextPosition == "FourTeamBluePositionFiveView" {
FourTeamBluePositionFiveView(fourTeamBlue:fourTeamBlue)
}else if nextPosition == "FourTeamBluePositionFourView" {
FourTeamBluePositionFourView(fourTeamBlue:fourTeamBlue)
}else if nextPosition == "FourTeamBluePositionThreeView" {
FourTeamBluePositionThreeView(fourTeamBlue:fourTeamBlue)
}else if nextPosition == "FourTeamBluePositionTwoView" {
FourTeamBluePositionTwoView(fourTeamBlue:fourTeamBlue)
}else if nextPosition == "FourTeamBluePositionOneView" {
FourTeamBluePositionOneView(fourTeamBlue:fourTeamBlue)
}
// Some code above deleted due to Character constraints
}
}
}
}
This View passes the Marker Latitude and Longitude to the Map View
struct FourTeamBluePositionTwoView: View {
@ObservedObject var fourTeamBlue = FourTeamBlue()
@State private var seePositionFourCard = false
@State private var disabled = true
@State private var fourTeamBluePositionTwoCardAnswer: String = ""
@State var color = 0;
var body: some View {
Form {
Section {
VStack(alignment: .center) {
Text ("Blue Team Position 2️⃣")
.font(.title)
.foregroundColor(.white)
.frame(maxWidth: .infinity, alignment: .center)
}
.background(Color.blue)
Section {
VStack(alignment: .center) {
TextField("answer then [RETURN]", text: $$fourTeamBluePositionTwoCardAnswer)
.keyboardType(.alphabet)
.disableAutocorrection(true)
.textInputAutocapitalization(.never)
.textCase(.lowercase)
.font(.title)
.foregroundColor(fourTeamBluePositionTwoCardAnswerColor(color: self.color))
.multilineTextAlignment(.center)
.onSubmit {
if (fourTeamBluePositionTwoCardAnswer == fourTeamBlue.fourTeamBlueSecondCardWords) {
disabled = false
color = 2
nextPosition = "FourTeamBluePositionFourView"
thisCardPositionLatitude = Double(fourTeamBlue.fourTeamBlueSecondCardLatitude)!
thisCardPositionLongitude = Double(fourTeamBlue.fourTeamBlueSecondCardLongitude)!
}
else {
disabled = true
color = 1
}
}
}
}
Section {
VStack(alignment: .center) {
Text(fourTeamBlue.fourTeamBlueSecondCardAnswerClue).kerning(10)
.font(.title)
.foregroundColor(.black)
.frame(maxWidth: .infinity, alignment: .center)
}
}
}
}
}
}
VStack(alignment: .center) {
Button(action: {
self.seeFinish.toggle()
}) {
Text("To Position 4️⃣ ▶️")
.font(.headline)
.foregroundColor(.black)
.frame(maxWidth: .infinity, alignment: .center)
.padding()
}.fullScreenCover(isPresented: $seeFinish) {
GameMapView()
}
}
.disabled(disabled)
}
}
func fourTeamBluePositionTwoCardAnswerColor(color: Int) -> Color
{
if(color == 1)
{
return Color.red;
}
else if(color == 2)
{
return Color.green;
}
else
{
return Color.black;
}
}