-1

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:)

George_th
  • 79
  • 1
  • 1
  • 5
  • 1
    r u testing this on simulator ? – Teja Nandamuri Oct 31 '15 at 14:22
  • are you sure the `meters` outlet is connected in your storyboard or xib file? – Michael Dautermann Oct 31 '15 at 14:22
  • I am testing it on an iPad and yes the meters label outlet is connected in my storyboard. – George_th Oct 31 '15 at 14:51
  • This isn't how you [eat an elephant](http://importblogkit.com/2015/05/how-do-you-eat-an-elephant/). From the title alone, I can tell that you've spent little to no effort narrowing down your problem. The only thing you can display in a label is a string. The only thing that Core Location gives you when you use it to calculate any sort of distance is a `CLDistance`. And there are a few steps in between too. So rather than writing a pile of code, seeing it doesn't work, then asking for help, why not try solving the problem like a programmer? – nhgrif Oct 31 '15 at 15:13
  • nhgrif, of course I know that I can only display a string in a label and I do convert the value into string but it has nothing to do with that.. The problem is that for some reason the delegate method is not called at all and thus I do not receive any values and I do not even receive the message for using the location services permission. In any case, you try not to be rude next type even if you are right and experienced programmer. As you see I spent a lot of effort to try and find a solution for some days now..Thank you anyway.. – George_th Nov 02 '15 at 12:00

2 Answers2

0

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).

Felix Lamouroux
  • 7,414
  • 29
  • 46
  • FelixLam, you are right about that, by mistake I had the delegate method inside the viewDidLoad, but nevertheless it did not solve the problem still..:( – George_th Oct 31 '15 at 15:49
0

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.

Community
  • 1
  • 1
George_th
  • 79
  • 1
  • 1
  • 5