From 88d046f9ebc94aa767800e856dbe013faa194b1b Mon Sep 17 00:00:00 2001 From: Exinferis Date: Wed, 9 Oct 2024 14:03:19 +0100 Subject: [PATCH] Add a map for the entered coodinates --- Code Relay/Code Relay/Localizable.xcstrings | 3 ++ Code Relay/Code Relay/NewLocationView.swift | 52 +++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/Code Relay/Code Relay/Localizable.xcstrings b/Code Relay/Code Relay/Localizable.xcstrings index 928bc34..5bf2479 100644 --- a/Code Relay/Code Relay/Localizable.xcstrings +++ b/Code Relay/Code Relay/Localizable.xcstrings @@ -60,6 +60,9 @@ }, "Save" : { + }, + "Selected Coordinate" : { + }, "Settings" : { "localizations" : { diff --git a/Code Relay/Code Relay/NewLocationView.swift b/Code Relay/Code Relay/NewLocationView.swift index 4aea77e..71adc70 100644 --- a/Code Relay/Code Relay/NewLocationView.swift +++ b/Code Relay/Code Relay/NewLocationView.swift @@ -7,6 +7,7 @@ import SwiftUI import CoreLocation +import MapKit public struct NewLocationView: View { @Environment(\.dismiss) var dismiss @@ -18,7 +19,19 @@ public struct NewLocationView: View { @State var long: Double? @State var phoneNumber: String + // Init the location with the current coordinates if applicable + @State var location: Location + public var body: some View { + + var region: Binding = Binding { + MKCoordinateRegion( + center: CLLocationCoordinate2D(latitude: lat ?? 0, longitude: long ?? 0), + span: MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05) + ) + } set: {_ in } + + NavigationStack { VStack { List { @@ -41,6 +54,19 @@ public struct NewLocationView: View { .onSubmit { field = field?.next } + + Spacer() + Map { + Marker("Selected Coordinate", coordinate: CLLocationCoordinate2D(latitude: lat ?? 0, longitude: long ?? 0)) + } + .cornerRadius(10) + .padding(.horizontal, 24) + .padding(.top, 24) + .padding(.bottom, 12) + .frame(height: 250) + .frame(maxWidth: .infinity) + Spacer() + Button { let coordinate: CLLocationCoordinate2D? = if let lat, let long { .init(latitude: lat, longitude: long) @@ -72,7 +98,33 @@ public struct NewLocationView: View { .padding(.horizontal, 24) } .navigationTitle("Add New Location") + }.onChange(of: lat ?? 0) { oldValue, newValue in + region.wrappedValue = MKCoordinateRegion( + center: CLLocationCoordinate2D(latitude: lat ?? 0, longitude: long ?? 0), + span: MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05) + ) } + .onChange(of: long ?? 0) { oldValue, newValue in + region.wrappedValue = MKCoordinateRegion( + center: CLLocationCoordinate2D(latitude: lat ?? 0, longitude: long ?? 0), + span: MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05) + ) + } + } + + init( + title: String, + details: String, + lat: Double? = nil, + long: Double? = nil, + phoneNumber: String + ) { + self.title = title + self.details = details + self.lat = lat + self.long = long + self.phoneNumber = phoneNumber + self.location = Location(title: "", details: "", coordinate: CLLocationCoordinate2D(latitude: lat ?? 0, longitude: long ?? 0), phoneNumber: "") } }