I pulled the coordinates from the user in my main view controller like this:
import CoreLocation
private let locationManager = CLLocationManager()
func findCurrentLocation() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation()
//locationManager.startUpdatingHeading
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
print("locations = \(locValue.latitude) \(locValue.longitude)")
}
I then have this URL in a separate file (my constants file)
let NEAREST_CITY_URL = BASE_URL + "nearest_city?lat={{LATITUDE}}&lon={{LONGITUDE}}&key=" + API_KEY
I need to get the latitude and longitude from the view controller into that URL. How would I pass it there?
I assume it needs to look something like this, but I can't figure out how to compile it without errors.
let NEAREST_CITY_URL = BASE_URL + "nearest_city?lat=\(MainVC.locationManager.locValue.latitude)&lon=\(MainVC.locationManager.locValue.longitude)&key=" + API_KEY
MainVC needs to set the data into your constants file, as a global variable (since you seem to desire using globals... eek). Then you can offer a NEAREST_CITY_URL that computes a string using that data.
In your constants file:
var userLoc : CLLocationCoordinate2D?
let NEAREST_CITY_URL = BASE_URL + "nearest_city?lat=\(userLoc.latitude ?? 0.0)&lon=\(userLoc.longitude ?? 0.0)&key=" + API_KEY
In your view controller:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
print("locations = \(locValue.latitude) \(locValue.longitude)")
userLoc = locValue
}
Now it's really bad to have a global constants file like you're doing... at the very least, place all your constants into a singleton class named Constants. But I'm just here to directly answer your question, so...
Related
class LocationManager: NSObject, CLLocationManagerDelegate {
private var onLocation: ((CLLocationCoordinate2D) -> Void)?
private let manager: CLLocationManager
override init() {
manager = CLLocationManager()
super.init()
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.delegate = self
manager.startUpdatingLocation()
}
public func getLocation(_ onLocation: ((CLLocationCoordinate2D) -> Void)?) {
self.onLocation = onLocation
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print(#function, locations)
guard let currentCoordinate = manager.location?.coordinate else {return}
onLocation?(currentCoordinate)
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(#function, error)
}
}
this code is not calling didUpdateLocation or didFailWithError. can anyone tell me what could be the problem here?
LocationManager().getLocation { coordinate in
print(#function, coordinate)
}
this is how i am calling it.
You need to retain the let manager = CLLocationManager() in your class as a property. Otherwise, it will be deallocated at the end of that function and hence none of its delegate methods will be called at all.
UPDATED
Another issue is the following code where you call getLocation. You need to retain LocationManager() in your client class otherwise the LocationManager will be deallocated at the end of that function.
private let locationManager = LocationManager()
locationManager.getLocation { coordinate in
print(#function, coordinate)
}
I have a class such as:
class LocationViewModel: NSObject, ObservableObject, CLLocationManagerDelegate {
#Published var lastSeenLocation: CLLocation?
#Published var currentPlacemark: CLPlacemark?
#Published var authorizationStatus: CLAuthorizationStatus
private let locationManager: CLLocationManager
override init() {
locationManager = CLLocationManager()
authorizationStatus = locationManager.authorizationStatus
super.init()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
func requestPermission() {
locationManager.requestAlwaysAuthorization()
}
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
authorizationStatus = manager.authorizationStatus
}
}
I'm trying to check if last seen location = cordinates using this code:
let radius: Double = 5 // miles
let userLocation = CLLocation(latitude: locationViewModel.lastSeenLocation?.coordinate.latitude, longitude: locationViewModel.lastSeenLocation?.coordinate.longitude)
let venueLocation = CLLocation(latitude: 51.500909, longitude: -0.177366)
let distanceInMeters = userLocation.distanceFromLocation(venueLocation)
let distanceInMiles = distanceInMeters * 0.00062137
if distanceInMiles < radius {
// user is near the venue
}
The only problem is, that I don't know how to run that code to check constantly. I was thinking .onChange but couldn't figure out how to test for lastSeenlocation in a class. What can I do?
I found a solution here.
For a quick overview:
//I put this code in my LocationViewModel class
func getUserLocation() {
locationManager.startUpdatingLocation()
locationManager.delegate = self
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.last {
print("latitude: \(location.coordinate.latitude), longitude: \(location.coordinate.longitude)")
}
}
//I put this code in my ContentView()
.onAppear {
locationViewModel.getUserLocation()
}
Then it prints your latitude & longitude every time your location changes.
I wrote a program that uses LocationManagerDelegate to display coordinates in the debug area whenever the current location changes. Got an error when retrieving coordinates
Can not use instance member 'locationManager' within property initializer; property initializers run before 'self' is available
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate{
var locationManager: CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
setUpLocationManager()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func setUpLocationManager() {
locationManager = CLLocationManager()
guard let locationManager = locationManager else {return}
locationManager.requestWhenInUseAuthorization()
let status = CLLocationManager.authorizationStatus()
if status == .authorizedWhenInUse {
locationManager.delegate = self
locationManager.distanceFilter = 10
locationManager.startUpdatingLocation()
printLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) ->Optional<Any> {
let location = locations.first
let latitude = location?.coordinate.latitude
let longitude = location?.coordinate.longitude
let latlong = [latitude, longitude]
return latlong
}
let myLocation = locationManager()
func printLocation() {
print("test\(myLocation)")
}
}
test (Function)
is output
let myLocation = locationManager ()
When you change to
let myLocation = locationManager
Your code contains a few mistakes.
The error occurs because you cannot execute the affected line on the top level of the class.
First of all you must not change signatures of delegate methods. This custom delegate method
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) ->Optional<Any> { ...
will never be called.
And apart from that why do you declare the return type as Any? although it's supposed to be [CLLocationCoordinate2D]?
Create the location manager immediately, replace
var locationManager: CLLocationManager!
with
let locationManager = CLLocationManager()
In setUpLocationManager() delete the lines
locationManager = CLLocationManager()
guard let locationManager = locationManager else {return} // this line is completely pointless anyway
printLocation()
The delegate method didUpdateLocations is called periodically and asynchronously. Print the result inside the method
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.first else { return }
let latitude = location.coordinate.latitude
let longitude = location.coordinate.longitude
let latlong = [latitude, longitude]
print("test", latlong)
}
Delete
let myLocation = locationManager()
func printLocation() {
print("test\(myLocation)")
}
i am working with Firebase Db, and i want to save lat,long value of device on Firebase. I had two value from locationManager()
This is my function locationManager()
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let lastLocation: CLLocation = locations[locations.count - 1]
let lat = String(format: "%.6f", lastLocation.coordinate.latitude)
let long = String(format: "%.6f", lastLocation.coordinate.longitude)
}
Next, i try TO save lat,long on Firebase:
ref = FIRDatabase.database().reference()
ref.setValue(lat)
Error: "Use of Unresolved Identifier".
How to get latitude and longitude value outside function LocationManager().
You can declare CLLocationManagerDelegate right where of your class definition.
class YourClass {
private var currentCoordinate: CLLocationCoordinate2D?
}
And in your location delegate method you assign the current value
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
currentCoordinate = manager.location.coordinate
}
// Here is some more info on the problem = Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
#IBOutlet var myMap : MKMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
myMap.showsUserLocation = true }
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let locValue:CLLocationCoordinate2D = manager.location!.coordinate
print("locations = \(locValue.latitude) \(locValue.longitude)")
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
{
print("Errors: " + error.localizedDescription)
}
#IBAction func satelliteView(){
myMap.mapType = MKMapType.Satellite
}
#IBAction func normalView(){
myMap.mapType = MKMapType.Standard
}
#IBAction func pin(sender: UILongPressGestureRecognizer) {
let location = sender.locationInView(self.myMap)
let lCoord = self.myMap.convertPoint(location, toCoordinateFromView: self.myMap)
let anno = MKPointAnnotation()
anno.coordinate = lCoord
anno.title = "store"
anno.subtitle = "loctaion of Store"
self.myMap.removeAnnotations(myMap.annotations)
self.myMap.addAnnotation(anno)
}
}
The simulator does not know your present location. Your need to let the simulator know your location.
In the simulator menu select Debug > Location > Custom Location
You can enter the Lat and Long of your physical location or any other location.
The following is from an app I wrote. You need to call something similar when locationManager(manager:didUpdateLocations: is called
func addPinAtCoordinate(latlong: Vector2D, plotting: Bool = defaultPlotState) {
if self.plotting && !plotting {
self.removeAnnotations(annotationStack)
annotationStack = []
self.plotting = plotting
}
let lat = latlong.x
let lon = latlong.y > 180.0 ? latlong.y - 360.0 : latlong.y
let coord = CLLocationCoordinate2DMake(lat, lon)
let region = self.regionThatFits(MKCoordinateRegionMake(coord, MKCoordinateSpan(latitudeDelta: mapSpanDegrees, longitudeDelta: mapSpanDegrees)))
if region.center.latitude != -180.0 || region.center.longitude != -180.0 {
self.setRegion(region, animated: true)
let note = MKPointAnnotation()
let annotPoint = CLLocationCoordinate2D(latitude: lat, longitude: lon)
note.coordinate = annotPoint
self.addAnnotation(note)
self.annotationStack.append(note)
if annotationStack.count > annotationStackLength {
self.removeAnnotation(annotationStack[0])
annotationStack.removeAtIndex(0)
}
}
else {
NSLog("Can't place invalid cordinate: ", latlong)
}
}
Most specifically useful relative to your problem are the lines creating a region and calling setRegion.