0
I am trying to implement input textfield for user to enter 'longitude' and 'latitude' to make annotation on the map. The scenario I want to achieve: When user click 'save' button after filling in both longitude and latitude. The annotation will appear based on the latitude and longitude inputs. I got 2 strings, "savedLong" and "savedLat". Please advice me if Im on the right track to solving my issue. Thank you! I get errors when I replace the Longitude and Latitude values with 'savedLong' and 'savedLat', example below is a portion of my current progress,
MyAnnotationItem(
coordinate: CLLocationCoordinate2D(
latitude: savedLat, //instead of numbers that is able to work
longitude: savedLong), //instead of numbers that is able to work
color: .green)]
Code below will save user input for both Latitude and Longitude,
VStack{
TextField("Latitude", text: $textFieldLat)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding()
.background(Color.gray.opacity(0.3).cornerRadius(10))
.foregroundColor(.red)
.font(.headline)
TextField("Longitude", text: $textFieldLong)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding()
.background(Color.gray.opacity(0.3).cornerRadius(10))
.foregroundColor(.red)
.font(.headline)
Button(action: {
savedLat = textFieldLat ////<-- saved as latitude to plot
savedLong = textFieldLong////<--- saved as longitude to plot
}, label: {
Text("Save".uppercased())
.padding()
.background(Color.gray.opacity(0.3).cornerRadius(10))
.foregroundColor(.red)
.font(.headline)
})
Text("Lat: "+savedLat+" Long: "+savedLong)
Code below is my current progress and is able to run.
import MapKit
import SwiftUI
struct ContentView: View {
@StateObject private var viewModel = ContentViewModel()
struct AnnotatedItem: Identifiable {
let id = UUID()
var name: String
var coordinate: CLLocationCoordinate2D
}
private var pointsOfInterest = [ //annotate on map
AnnotatedItem(name: "Location A", coordinate:
.init(latitude: 5.76123, longitude: 156.52318))
]
var body: some View {
Map(coordinateRegion: $viewModel.region,
showsUserLocation: true, annotationItems: pointsOfInterest){
item in
MapMarker(coordinate: item.coordinate)
}
.ignoresSafeArea() //full screen
.accentColor(Color(.systemPink)) //color of icon
.onAppear {
viewModel.checkIfLocationServicesIsEnabled()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
enum MapDetails {
static let startingLocation = CLLocationCoordinate2D(latitude: 5.76123, longitude: 156.52318)
static let defaultSpan = MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)
}
final class ContentViewModel: NSObject, ObservableObject,
CLLocationManagerDelegate {
@Published var region = MKCoordinateRegion(center: MapDetails.startingLocation,
span: MapDetails.defaultSpan)
var locationManager: CLLocationManager?
func checkIfLocationServicesIsEnabled(){
if CLLocationManager.locationServicesEnabled(){
locationManager = CLLocationManager()
locationManager!.delegate = self
}else{
print("Show map is off")
}
}
private func checkLocationAuthorization(){
guard let locationManager = locationManager else {return}
switch locationManager.authorizationStatus{
case .notDetermined:
locationManager.requestWhenInUseAuthorization()
case .restricted:
print("Location restricted")
case .denied:
print("Location denied")
case .authorizedAlways, .authorizedWhenInUse:
region = MKCoordinateRegion(center: locationManager.location!.coordinate,
span: MapDetails.defaultSpan)
break
@unknown default:
break
}
}
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
checkLocationAuthorization()
}
}
}