SwiftUI - Location in API call with CLLocationManager and CLGeocoder - swiftui

I'm struggling with this for a long time without finding where I'm wrong (I know I'm wrong).
I have one API call with the location of the phone (this one is working), but I want the same API call with a manual location entered by a textfield (using Geocoding for retrieving Lat/Long). The geocoding part is ok and updated but not passed in the API call.
I also want this API call to be triggered when the TextField is cleared by the dedicated button back with the phone location.
Please, what am I missing? Thanks for your help.
UPDATE: This works on Xcode 12.2 beta 2 and should work on Xcode 12.0.1
This is the code:
My Model
import Foundation
struct MyModel: Codable {
let value: Double
}
My ViewModel
import Foundation
import SwiftUI
import Combine
final class MyViewModel: ObservableObject {
#Published var state = State.ready
#Published var value: MyModel = MyModel(value: 0.0)
#Published var manualLocation: String {
didSet {
UserDefaults.standard.set(manualLocation, forKey: "manualLocation")
}
}
#EnvironmentObject var coordinates: Coordinates
init() {
manualLocation = UserDefaults.standard.string(forKey: "manualLocation") ?? ""
}
enum State {
case ready
case loading(Cancellable)
case loaded
case error(Error)
}
private var url: URL {
get {
return URL(string: "https://myapi.com&lat=\(coordinates.latitude)&lon=\(coordinates.longitude)")!
}
}
let urlSession = URLSession.shared
var dataTask: AnyPublisher<MyModel, Error> {
self.urlSession
.dataTaskPublisher(for: self.url)
.map { $0.data }
.decode(type: MyModel.self, decoder: JSONDecoder())
.receive(on: RunLoop.main)
.eraseToAnyPublisher()
}
func load(){
assert(Thread.isMainThread)
self.state = .loading(self.dataTask.sink(
receiveCompletion: { completion in
switch completion {
case .finished:
print("⚠️ API Call finished")
break
case let .failure(error):
print("❌ API Call failure")
self.state = .error(error)
}
},
receiveValue: { value in
self.state = .loaded
self.value = value
print("👍 API Call loaded")
}
))
}
}
The Location Manager
import Foundation
import SwiftUI
import Combine
import CoreLocation
import MapKit
final class Coordinates: NSObject, ObservableObject {
#EnvironmentObject var myViewModel: MyViewModel
#Published var latitude: Double = 0.0
#Published var longitude: Double = 0.0
#Published var placemark: CLPlacemark? {
willSet { objectWillChange.send() }
}
private let locationManager = CLLocationManager()
private let geocoder = CLGeocoder()
override init() {
super.init()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
}
deinit {
locationManager.stopUpdatingLocation()
}
}
extension Coordinates: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else { return }
latitude = location.coordinate.latitude
longitude = location.coordinate.longitude
geocoder.reverseGeocodeLocation(location, completionHandler: { (places, error) in
self.placemark = places?[0]
})
self.locationManager.stopUpdatingLocation()
}
}
extension Coordinates {
func getLocation(from address: String, completion: #escaping (_ location: CLLocationCoordinate2D?)-> Void) {
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(address) { (placemarks, error) in
guard let placemarks = placemarks,
let location = placemarks.first?.location?.coordinate else {
completion(nil)
return
}
completion(location)
}
}
}
The View
import Foundation
import SwiftUI
struct MyView: View {
#EnvironmentObject var myViewModel: MyViewModel
#EnvironmentObject var coordinates: Coordinates
private var icon: Image { return Image(systemName: "location.fill") }
var body: some View {
VStack{
VStack{
Text("\(icon) \(coordinates.placemark?.locality ?? "Unknown location")")
Text("Latitude: \(coordinates.latitude)")
Text("Longitude: \(coordinates.longitude)")
}
VStack{
Text("UV Index: \(myViewModel.value.value)")
.disableAutocorrection(true)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding()
}
HStack{
TextField("Manual location", text: $myViewModel.manualLocation)
if !myViewModel.manualLocation.isEmpty{
Button(action: { clear() }) { Image(systemName: "xmark.circle.fill").foregroundColor(.gray) }
}
}
}.padding()
}
func commit() {
coordinates.getLocation(from: self.myViewModel.manualLocation) { places in
coordinates.latitude = places?.latitude ?? 0.0
coordinates.longitude = places?.longitude ?? 0.0
}
myViewModel.load()
}
func clear() {
myViewModel.manualLocation = ""
myViewModel.load()
}
}

Related

Sunkit on SwiftUi: help needed to use CoreLocation to determine the sunrise, golden hour etc

Hello
I want to make an app to catch runrise and goldenhour in SwiftUI.
I don't have errors, but it doesn't work neither.
The result is saying the following:
Current Location Unknown
Time Zone Unknown
I use the SunKit package Dependency.
I don't get an error.
Dependency used is SunKit:
https://github.com/Sunlitt/SunKit
Help is appreciated. As it doesn't work
Normally I should have the core location to fill in the location and specify the hour.
But it aint happening.
I don't know what is happening.
import SwiftUI
import SunKit
import CoreLocation
class LocationManagerDelegate: NSObject, CLLocationManagerDelegate {
var contentView: ContentView?
var locationManager = CLLocationManager()
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else { return }
if let contentView = contentView {
do {
try contentView.sun.setLocation(location)
contentView.location = location
} catch let error {
print(error)
}
}
let geocoder = CLGeocoder()
geocoder.reverseGeocodeLocation(location) { (placemarks, error) in
if let error = error {
print(error)
return
}
if let placemarks = placemarks, let placemark = placemarks.first, let timeZone = placemark.timeZone {
if let contentView = self.contentView {
contentView.timeZone = timeZone
print("TimeZone: \(timeZone.identifier) offset : \(timeZone.secondsFromGMT())")
do { try contentView.sun.setTimeZone(Double(timeZone.secondsFromGMT()) / 3600.0)
} catch let error {
print(error)
}
}
}
}
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse {
locationManager.startUpdatingLocation()
}
}
}
struct ContentView: View {
let sun: Sun = Sun.common
let timeFormatter: DateFormatter = {
let tf = DateFormatter()
tf.dateFormat = "HH:mm"
return tf
}()
#State var locationManager = CLLocationManager()
#State var locationManagerDelegate = LocationManagerDelegate()
#State public var location: CLLocation?
#State public var timeZone: TimeZone?
init() {
locationManagerDelegate.contentView = self
locationManager.delegate = locationManagerDelegate
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
}
var body: some View {
VStack {
Text("Azimuth: \(sun.azimuth)°")
Text("Elevation: \(sun.elevation.degrees)°")
Text("Sunrise time: \(timeFormatter.string(from: sun.sunrise))")
if let location = location {
Text("Current Location: \(location.coordinate.latitude), \(location.coordinate.longitude)")
} else {
Text("Current Location: Unknown")
}
if let timeZone = timeZone {
Text("Time Zone: \(timeZone.abbreviation() ?? "Unknown")")
} else {
Text("Time Zone: Unknown")
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Stil more details needed ?

SwiftUI: Trigger view update after user pans the map

I'm new to Swift/SwiftUI, and so please forgive me if this is trivial.
In an app I am attempting to retrieve the user's location, and fetch nearby websites from Wikipedia to display. A simple example is below:
//
// ContentView.swift
// MRE
//
// Created by Philipp Maier on 2/25/22.
//
import SwiftUI
import CoreLocationUI
import MapKit
import Foundation
struct ContentView: View {
enum ViewState {
case waiting, fetching
}
#State private var viewState = ViewState.waiting
#StateObject private var viewModel = LocationViewModel()
// some values to initialize
#State var listEntries = [
Geosearch(pageid: 45348219,
title: "Addison Apartments",
lat: 35.21388888888889,
lon: -80.84472222222222,
dist: 363.7 ),
Geosearch(pageid: 35914731,
title: "Midtown Park (Charlotte, North Carolina)",
lat: 35.2108,
lon: -80.8363,
dist: 1034.5 )
]
var body: some View {
NavigationView{
VStack{
ZStack(alignment: .leading){
Map(coordinateRegion: $viewModel.mapRegion, showsUserLocation: true)
.frame(height: 300)
LocationButton(.currentLocation, action: {
viewModel.requestAllowLocationPermission()
viewState = .fetching
print("Button Press: Latitude: \(viewModel.mapRegion.center.latitude), Longitude: \(viewModel.mapRegion.center.longitude)")
})
}
HStack{
Text("Latitude: \(viewModel.mapRegion.center.latitude), Longitude: \(viewModel.mapRegion.center.longitude)")
}
switch viewState {
case .waiting:
Text("Waiting for your location")
Spacer()
case .fetching:
List{
ForEach(listEntries) {location in
NavigationLink(destination: Text("Target") ) {
HStack(alignment: .top){
Text(location.title)
}
}
.task{
await fetchNearbyLandmarks(lat: viewModel.mapRegion.center.latitude, lon: viewModel.mapRegion.center.longitude)
}
}
}
}
}
}
}
func fetchNearbyLandmarks(lat: Double, lon: Double) async {
let urlString = "https://en.wikipedia.org/w/api.php?action=query&list=geosearch&gscoord=\(lat)%7C\(lon)&gsradius=5000&gslimit=25&format=json"
print(urlString)
guard let url = URL(string: urlString) else {
print("Bad URL: \(urlString)")
return
}
do {
let (data, _) = try await URLSession.shared.data(from: url)
let items = try JSONDecoder().decode(wikipediaResult.self, from: data)
listEntries = items.query.geosearch
print ("Loadingstate: Loaded")
} catch {
print ("Loadingstate: Failed")
}
}
}
final class LocationViewModel: NSObject, ObservableObject, CLLocationManagerDelegate {
#Published var mapRegion = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 40, longitude: -80.5), span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5))
let locationManager = CLLocationManager()
override init() {
super.init()
locationManager.delegate = self
}
func requestAllowLocationPermission() {
locationManager.requestLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let latestLocation = locations.first else {
print("Location Error #1")
return
}
DispatchQueue.main.async {
self.mapRegion = MKCoordinateRegion(center: latestLocation.coordinate, span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1))
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Location Error #2:")
print(error.localizedDescription)
}
}
struct wikipediaResult: Codable {
let batchcomplete: String
let query: Query
}
struct Query: Codable {
let geosearch: [ Geosearch ]
}
struct Geosearch: Codable, Identifiable {
var id: Int { pageid }
let pageid: Int
let title: String
let lat: Double
let lon: Double
let dist: Double
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
The core function of the app works, in that once the user pushes the "Location" button, the list of Wikipedia pages underneath updates. I've also added to Text fields to track the center of the map as the user pans around.
Here's my issue: If I pan the map, the list of locations underneath does not update. However, once I click on a link to display the page, and hit the "back" button, the updated list is built "correctly", i.e. with the new coordinates.
I'm sure that I'm overlooking something simple, but how can I track panning of the user and dynamically adjust the list underneath "in real time"?
Thanks, Philipp
For being new to Swift/SwiftUI this is quite cool!
You want to refetch when the coordinates of your Map change. So you could use .onChange, e.g.on the ZStack of the Map:
.onChange(of: viewModel.mapRegion.center.latitude) { _ in
Task {
await fetchNearbyLandmarks(lat: viewModel.mapRegion.center.latitude, lon: viewModel.mapRegion.center.longitude)
}
}
Generally this works, but it might be fetching too often (on every change of latitude). So you might want to add some kind of delay, or try to figure out when the drag ended.

SwiftUI How to toggle a Bool in a Struct from an ObservableObject class and show alert to notify user

I have a method in my class that opens a map when given an address string. Trying to show an alert in a view by toggling a boolean in a method in the class. I can't figure out how to toggle the boolean in the class method. This is what I tried. The Published bool in class method updates but does not update in the View. I did put up a repo of just this feature if anybody wants to play around with it.
https://github.com/Ongomobile/LocationTest/tree/main/LocationTest
import SwiftUI
#main
struct LocationTestApp: App {
var body: some Scene {
WindowGroup {
ContentView(location: LocationManager())
}
}
}
Here is my Class:
import UIKit
import MapKit
import CoreLocation
import Combine
class LocationManager: NSObject, ObservableObject {
var locationManager = CLLocationManager()
lazy var geocoder = CLGeocoder()
#Published var locationString = "1140"
// #Published var locationString = "1 apple park way cupertino"
#Published var currentAddress = ""
#Published var isValid: Bool = true
func openMapWithAddress () {
geocoder.geocodeAddressString(locationString) { placemarks, error in
if let error = error {
self.isValid = false
// prints false but does not update
print("isValid")
print(error.localizedDescription)
}
guard let placemark = placemarks?.first else {
return
}
guard let lat = placemark.location?.coordinate.latitude else{return}
guard let lon = placemark.location?.coordinate.longitude else{return}
let coords = CLLocationCoordinate2DMake(lat, lon)
let place = MKPlacemark(coordinate: coords)
let mapItem = MKMapItem(placemark: place)
mapItem.name = self.locationString
mapItem.openInMaps(launchOptions: nil)
}
}
}
Here is the view:
import SwiftUI
struct ContentView: View {
#ObservedObject var locationManager = LocationManager()
#State private var showingAlert = false
var body: some View {
Button {
locationManager.openMapWithAddress()
} label: {
Text("Get Map")
}
.alert(isPresented: $showingAlert) {
Alert(title: Text("Important message"), message:
Text("Enter a valid address"), dismissButton:
.default(Text("OK")))
}
}
}
I updated this answer to reflect some refactor help that I got from #rlong405 I put up a repository with this solution maybe it could help others.
OpenMapsInSwiftUI
import SwiftUI
struct ContentView: View {
#ObservedObject var locationManager = LocationManager()
var body: some View {
VStack{
Form{
Section {
Text("Enter Address")
TextField("", text: $locationManager.locationString)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding(.horizontal)
}
Button {
locationManager.openMapWithAddress()
} label: {
Text("Get Map")
}
.alert(isPresented: $locationManager.invalid) {
Alert(title: Text("Important message"), message:
Text("Enter a valid address"), dismissButton:
.default(Text("OK"), action:{
locationManager.invalid = false
locationManager.locationString = ""
}))
}
}
}
}
}
import UIKit
import MapKit
import CoreLocation
import Combine
class LocationManager: NSObject, ObservableObject {
lazy var geocoder = CLGeocoder()
#Published var locationString = ""
#Published var invalid: Bool = false
func openMapWithAddress () {
geocoder.geocodeAddressString(locationString) { placemarks, error in
if let error = error {
DispatchQueue.main.async {
self.invalid = true
}
print(error.localizedDescription)
}
guard let placemark = placemarks?.first else {
return
}
guard let lat = placemark.location?.coordinate.latitude else{return}
guard let lon = placemark.location?.coordinate.longitude else{return}
let coords = CLLocationCoordinate2DMake(lat, lon)
let place = MKPlacemark(coordinate: coords)
let mapItem = MKMapItem(placemark: place)
mapItem.name = self.locationString
mapItem.openInMaps(launchOptions: nil)
}
}
}

Using mapViewDidChangeVisibleRegion with CLGeocoder to return name of the country

I just want to return the name of the country where the black circle stops.
[1]: https://i.stack.imgur.com/Re5FK.jpg
Example in the picture above
I know it can be done by CLGeocoder and CLPlacemark along with Mapkit, and I have tried a lot but always got an error and the app would crash.
my code
import MapKit
import SwiftUI
struct MapView: UIViewRepresentable {
#Binding var centralCoordinate: CLLocationCoordinate2D
var anotations: [MKPointAnnotation]
func makeUIView(context: Context) -> MKMapView{
let mapview = MKMapView()
mapview.delegate = context.coordinator
return mapview
}
func updateUIView(_ view: MKMapView, context: Context) {
if anotations.count != view.annotations.count {
view.removeAnnotations(view.annotations)
view.addAnnotations(anotations)
}
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, MKMapViewDelegate {
var parent: MapView
init(_ parent: MapView) {
self.parent = parent
}
func mapViewDidChangeVisibleRegion(_ mapView: MKMapView) {
parent.centralCoordinate = mapView.centerCoordinate
let geoCoder = CLGeocoder()
let location = CLLocation(latitude: mapView.centerCoordinate.latitude, longitude: mapView.centerCoordinate.longitude)
geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in
// Place details
var placeMark: CLPlacemark!
placeMark = placemarks?[1]
// Country
if let country = placeMark.country {
print(country)
}
})
}
}
}
extension MKPointAnnotation{
static var eample: MKPointAnnotation {
let annotaton = MKPointAnnotation()
annotaton.title = "London"
annotaton.subtitle = "Home TO 2012 Summer Olampics"
annotaton.coordinate = CLLocationCoordinate2D(latitude: 51.5, longitude: -0.13)
return annotaton
}
}
struct MapView_Previews: PreviewProvider {
static var previews: some View {
MapView(centralCoordinate: .constant(MKPointAnnotation.eample.coordinate), anotations: [MKPointAnnotation.eample])
}
}
my view code
import MapKit
import SwiftUI
struct MapViewIntegration: View {
#State private var centercoordinate = CLLocationCoordinate2D()
#State private var locations = [MKPointAnnotation]()
var body: some View {
ZStack {
MapView(centralCoordinate: $centercoordinate,anotations: locations)
.edgesIgnoringSafeArea(.all)
Circle()
.opacity(0.3)
.frame(width: 32, height: 32)
VStack{
Spacer()
HStack{
Spacer()
Button(action: {
let newLocations = MKPointAnnotation()
newLocations.coordinate = self.centercoordinate
self.locations.append(newLocations)
}) {
Image(systemName: "plus")
}.padding()
.background(Color.white)
.font(.title)
.clipShape(Circle())
.padding(.trailing)
}
}
}
}
}
struct MapViewIntegration_Previews: PreviewProvider {
static var previews: some View {
MapViewIntegration()
}
}
please help,
thanks in advance
this answer was given by #SanzioAngeli
Replace this code with
func mapViewDidChangeVisibleRegion(_ mapView: MKMapView) {
parent.centralCoordinate = mapView.centerCoordinate
let geoCoder = CLGeocoder()
let location = CLLocation(latitude: mapView.centerCoordinate.latitude, longitude: mapView.centerCoordinate.longitude)
geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in
// Place details
var placeMark: CLPlacemark!
placeMark = placemarks?[1]
// Country
if let country = placeMark.country {
print(country)
}
})
}
}
this
func mapViewDidChangeVisibleRegion(_ mapView: MKMapView) {
parent.centralCoordinate = mapView.centerCoordinate
let geoCoder = CLGeocoder()
let location = CLLocation(latitude: mapView.centerCoordinate.latitude, longitude: mapView.centerCoordinate.longitude)
geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in
// Place details
var placeMark: CLPlacemark!
placeMark = placemarks?[0]
// Country
if let country = placeMark.country {
print(country)
}
})
}
}
#SanzioAngeli thanks.
Remember not to send too many requests at a time all the requests will fail and throw a fatal error.

How to make the bottom button follow the keyboard display in SwiftUI

With the help of the following, I was able to follow the button on the keyboard display.
However, animation cannot be applied well.
How to show complete List when keyboard is showing up in SwiftUI
import SwiftUI
import Combine
import UIKit
class KeyboardResponder: ObservableObject {
let willset = PassthroughSubject<CGFloat, Never>()
private var _center: NotificationCenter
#Published var currentHeight: CGFloat = 0
var keyboardDuration: TimeInterval = 0
init(center: NotificationCenter = .default) {
_center = center
_center.addObserver(self, selector: #selector(keyBoardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
_center.addObserver(self, selector: #selector(keyBoardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
}
deinit {
_center.removeObserver(self)
}
#objc func keyBoardWillShow(notification: Notification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
currentHeight = keyboardSize.height
guard let duration:TimeInterval = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double else { return }
keyboardDuration = duration
}
}
#objc func keyBoardWillHide(notification: Notification) {
currentHeight = 0
}
}
import SwiftUI
struct Content: View {
#ObservedObject var keyboard = KeyboardResponder()
var body: some View {
VStack {
Text("text")
Spacer()
NavigationLink(destination: SubContentView()) {
Text("Done")
}
}
.padding(.bottom, keyboard.currentHeight)
animation(Animation.easeInOut(duration: keyboard.keyboardDuration))
}
}
enter image description here
Your main problem, is that you are using an implicit animation. Not only it may be animating things you may not want to animate, but also, you should never apply .animation() on containers. Of the few warnings in SwiftUI's documentation, this is one of them:
Use this modifier on leaf views rather than container views. The
animation applies to all child views within this view; calling
animation(_:) on a container view can lead to unbounded scope.
Source: https://developer.apple.com/documentation/swiftui/view/3278508-animation
The modified code removes the implicit .animation() call and replaces it with two implicit withAnimation closures.
I also replaced keyboardFrameEndUserInfoKey with keyboardFrameEndUserInfoKey, second calls were giving useless geometry.
class KeyboardResponder: ObservableObject {
let willset = PassthroughSubject<CGFloat, Never>()
private var _center: NotificationCenter
#Published var currentHeight: CGFloat = 0
var keyboardDuration: TimeInterval = 0
init(center: NotificationCenter = .default) {
_center = center
_center.addObserver(self, selector: #selector(keyBoardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
_center.addObserver(self, selector: #selector(keyBoardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
}
deinit {
_center.removeObserver(self)
}
#objc func keyBoardWillShow(notification: Notification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
guard let duration:TimeInterval = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double else { return }
keyboardDuration = duration
withAnimation(.easeInOut(duration: duration)) {
self.currentHeight = keyboardSize.height
}
}
}
#objc func keyBoardWillHide(notification: Notification) {
guard let duration:TimeInterval = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double else { return }
withAnimation(.easeInOut(duration: duration)) {
currentHeight = 0
}
}
}
struct ContentView: View {
#ObservedObject var keyboard = KeyboardResponder()
var body: some View {
return VStack {
Text("text \(keyboard.currentHeight)")
TextField("Enter text", text: .constant(""))
Spacer()
NavigationLink(destination: Text("SubContentView()")) {
Text("Done")
}
}
.padding(.bottom, keyboard.currentHeight)
// .animation(Animation.easeInOut(duration: keyboard.keyboardDuration))
}
}