Value of distance between 2 CLLocation points does not appear on label text - ios

I am trying to calculate the distance from the starting position I am (when i first start the app) to the current position and present it on a label (meters.text) but nothing appears on it..
Here is my code:
import UIKit
import CoreLocation
class CorrectViewController: UIViewController,CLLocationManagerDelegate {
#IBOutlet weak var meters: UILabel!
var wr: Int = 0
var distance = CLLocationDistance()
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization() //starts when i use the app
self.locationManager.distanceFilter = kCLHeadingFilterNone
self.locationManager.startUpdatingLocation()
}
//locatioManager delegate method
func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation) {
let currentLocation: CLLocation = newLocation
let startLocation: CLLocation = oldLocation
distance = startLocation.distanceFromLocation(currentLocation)
let tripString = NSString(format: "%.2f", distance)
meters.text = ("\(tripString)")
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
meters.text = ("error!")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
Any help is appreciated:)

It appears that the location update delegate method is inside the viewDidLoad function and hence defines a local function rather than implementing the delegate method.
Move the following method out of the viewDidLoad scope:
locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation)
In addition you are not requesting permission to use the users location. Make sure to do that and add the necessary string to your info plist.
Also: oldLocation will always be the previous location (not your initial one).

Alright, after a lot of searching and "trial and error" I found the solution here.
Instead of using the method didUpdateToLocation the method didUpdateLocations should be used. Moreover, it is really important to put NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription keys in the .plist file (value may be an additional message that will be presented in location alert). These keys are required in iOS 8.

Related

How to add current timestamp in camera in ios app

Something like this I want to add current timestamp when recording is done in the video and save into camera roll.
**Note **: I have successfully added current location and timestamp in AVVideoPreviewLayer but it is static after exporting video is show static time does not show running timestamp
Try creating a layer of previewLayer: UIView with the components you want to have on camera.
Add this layer to currently running session of AVCaptureVideoPreviewLayer.
This link might help you to achieve your problem
Adding objects over camera view in app (Swift 3) not under camera view?
I tried something out. I guess you want to get latitude, longitude and the timestamp.
You find an example for latitude and longitude below.
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.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func showLocation(_ sender: Any) {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
if(CLLocationManager.locationServicesEnabled()){
locationManager.startUpdatingLocation()
}
locationManager.stopUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation:CLLocation = locations[0] as CLLocation
print("user latitude = \(userLocation.coordinate.latitude)")
print("user longitude = \(userLocation.coordinate.longitude)")
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
{
print("Error \(error)")
}
}
To get the current time you can use something like this:
let date = NSDate()
print(date)
The result for both in console should look something like this:
2017-12-17 21:45:39 +0000
user latitude = 37.3322499
user longitude = -122.056264
(The time looks like this in my example because im from EU but you can convert it to what you want)
You can add this to a separate view and bring it to front with
view.bringSubview(toFront: YourView)
I hope this is helpful!

location manager not updating in swift ios10

I am new to swift and am trying to get a basic program that display the longitude and latitutde.
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
#IBOutlet weak var latLabel: UILabel!
#IBOutlet weak var longLabel: UILabel!
#IBOutlet weak var addLabel: UILabel!
var lm = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
lm = CLLocationManager()
}
#IBAction func getCurrentLocation(_ sender: AnyObject) {
lm.delegate = self
lm.desiredAccuracy = kCLLocationAccuracyBest
lm.startUpdatingLocation()
print("loc")
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print ("error")
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print ("location")
let length = locations.count
let curLoc = locations[length-0]
latLabel.text = String(curLoc.coordinate.latitude)
print ("loccation")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
You are missing two things.
you have to ask for permission using requestAlwaysAuthorization or requestWhenInUseAuthorization().
So when you are allocating your location manager in viewdidlod, Do like this
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
You need to add information as #Anbu.Karthik has suggested in his answer.
Use NSLocationAlwaysUsageDescription for apps that want to use the
device's location even when the app is not open and being used.
Use NSLocationWhenInUseUsageDescription for apps that want to use the
device's location only when the app is open and in use.
check once are you added the following information your plist
Location :
Key : Privacy - Location Always Usage Description
Value : $(PRODUCT_NAME) location use
Key : Privacy - Location When In Use Usage Description
Value : $(PRODUCT_NAME) location use
for more information see this
One other issue that I found caused the location update callback not to be called, with no obvious error message given is that it must not be a private function (which is obvious once you finally notice it, of course...). I realise the OP's one is not but this is a common place to end up if you are having issues, so it may help someone.
So the following will cause the function not to be called without giving any obvious error indication like crashing the app etc:
private func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print ("location")
let length = locations.count
let curLoc = locations[length-0]
latLabel.text = String(curLoc.coordinate.latitude)
print ("loccation")
}
Removing the 'private' keyword, as in the OP's code, will allow it to be called.

CLLocationManager issue

I'm learning about user localization in swift. I'm trying to print localization on console (later I will use it for info on label, so I want to check if it works), but I have no idea why it prints nothing. Even if delete conversions to string, and leave just for printing anything, it still doesn't work. Please help.
Yes, I added NSLocationAlwaysUsageDescription and
NSLocationWhenInUseUsageDescription.
import UIKit
import CoreLocation
import MapKit
class ViewController: UIViewController, CLLocationManagerDelegate {
var locationManager = CLLocationManager()
var myPosition = CLLocationCoordinate2D()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation) {
print("Got location: \(newLocation.coordinate.latitude), \(newLocation.coordinate.longitude)")
myPosition = newLocation.coordinate
locationManager.stopUpdatingLocation()
}
}
didUpdateToLocation is an outdated method of the CLLocationManagerDelegate. It is available in 10.6 and earlier. Instead, use didUpdateLocations, which will return an array of all location objects in order of time recent. You then access the latest, newest location be grabbing the last object in the returned array.
So try this
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
var latestLocation: CLLocation = locations.last;
print("Got location: \(latestLocation.coordinate.latitude), \(latestLocation.coordinate.longitude)")
}
tell me how it goes.

CLLocationCoordinates from locationManager didUpdateLocations

I am trying to use CoreLocation (once permission is granted) to get a user's CLLocationCoordinate2D, so that I can pass that information to an Uber deeplink (after a UIButton within my TableView is pressed).
I've figured out how to get the coordinates as CLLocations, and turn them into CLLocationCoordinates2D in the didUpdateLocations method, but can't seem to transfer them over to my buttonPressed function.
Can anyone explain how I can properly transfer the coordinates info to the uberButtonPressed method? I am also confused about how to get the locationManager to stop updating location once a suitable location is determined. Any help is much appreciated. By the way I am using this to instantiate Uber: https://github.com/kirby/uber
import UIKit
import CoreLocation
import MapKit
class ATableViewController: UITableViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
var location: CLLocation?
var coordinate: CLLocationCoordinate2D?
// Implemented tableView methods etc here...
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
let newLocation = locations.last as! CLLocation
println("DID UPDATE LOCATIONS \(newLocation)")
location = newLocation
coordinate = location!.coordinate
println("WE HAVE THE COORDINATES \(coordinate!.latitude) and \(coordinate!.longitude)") // this prints along with DID UPDATE LOCATIONS
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
println("error:" + error.localizedDescription)
}
func uberButtonPressed(sender: UIButton!) {
let senderButton = sender
println(senderButton.tag)
let authStatus: CLAuthorizationStatus = CLLocationManager.authorizationStatus()
if authStatus == .NotDetermined {
locationManager.requestWhenInUseAuthorization()
return
}
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
var pickupLocation = coordinate! // fatal error: unexpectedly found nil while unwrapping an Optional value
println(pickupLocation)
// // Create an Uber instance
// var uber = Uber(pickupLocation: pickupLocation)
//
// Set a few optional properties
// uber.pickupNickname = "OK"
//
// uber.dropoffLocation = CLLocationCoordinate2D(latitude: 47.591351, longitude: -122.332271)
// uber.dropoffNickname = "whatever"
//
// // Let's do it!
// uber.deepLink()
//
}
You should move var pickupLocation = coordinate! into your didUpdateLocations. Once assigned, you can call locationManager.stopUpdatingLocation() also from inside 'didUpdateLocation or your value for coordinate with keep updating. After stopping the locationManager, call a NEW function to run the rest of your code currently in func uberButtonPressed
I had the same problem.
Instead of calling the delegate method like this:
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
debugPrint("NOT CALLED-- didUpdateLocations AnyObject")
}
I changed into:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
debugPrint("CALLED-- didUpdateLocations CLLocation")
}
The problem is that you are unwrapping coordinate which is a CLLocationCoordinate2D? with a !. Since coordinate is from location which is a CLLocation?. It is possible that coordinate is nil and location is nil, especially before location services have had a chance to kick in. Only run the rest of uberButtonPressed: for the case where coordinate and location are not nil by using if let currentCoordinate = coordinate?, and in that case, call locationManager.stopUpdatingLocation().

Finding Altitude in Swift

I've been having trouble finding a way to get the altitude of the device. Could someone give me a pointer or put together a short script that gets the altitude of the device and prints it? Only in swift. Thanks!
Import CoreLocation
import CoreLocation
Create a locationManger variable
var locationManager:CLLocationManager = CLLocationManager()
Initialize and start updating location
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager = CLLocationManager()
locationManager.requestWhenInUseAuthorization()
self.locationManager.delegate = self
self.locationManager.distanceFilter = kCLDistanceFilterNone
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager!, didUpdateToLocation newLocation: CLLocation!, fromLocation oldLocation: CLLocation!) {
var alt = newLocation.altitude
println("\(alt)")
manager.stopUpdatingLocation()
}

Resources