hi folks! I'm new to swift but started a project with my son. we want to run this little code for converting GK4 coordinates into latitude longitude, store them in a listing and show them by click in Apple Maps. we've import a gausskrueger repository to our project folder. that's working surprisedly:) we've got some output in simulator and apple map open but sadly its showing 0.00000 N, 0.00000 and for sure the coordinates are in the blue ocean. can someone have a look?
import SwiftUI
import MapKit
import GaussKrueger
struct CoordinateConverterView: View {
@State private var xInput = ""
@State private var yInput = ""
@State private var coordinates: [String] = []
var body: some View {
VStack {
HStack {
TextField("X", text: $xInput)
TextField("Y", text: $yInput)
}
Button("Convert") {
if let x = Double(xInput), let y = Double(yInput) {
let coord = GKCoordinate(x: x, y: y).asWGS
let coordinateString = "Latitude: (String(describing: coord?.latitude)) Longitude: (String(describing: coord?.longitude))"
coordinates.append(coordinateString)
xInput = ""
yInput = ""
}
}
List(coordinates, id: .self) { coordinate in
HStack {
Text(coordinate)
.font(.system(size: 10))
Spacer()
Button(action: {
self.showInMaps(coordinate: coordinate)
}, label: {
Image(systemName: "mappin.and.ellipse")
})
}
}
}
}
func showInMaps(coordinate: String) {
let scanner = Scanner(string: coordinate)
var latitude: Double = 0
var longitude: Double = 0
scanner.scanString("Latitude: ", into: nil)
scanner.scanDouble(&latitude)
scanner.scanString(", Longitude: ", into: nil)
scanner.scanDouble(&longitude)
let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let mapItem = MKMapItem(placemark: MKPlacemark(coordinate: coordinate, addressDictionary: nil))
mapItem.openInMaps(launchOptions: nil)
}
}