0

In this code I receive a wrong distance, _latitude and _longtitude are NSString. Any suggestion to correct this?

CLLocationManager *locationManager = [[CLLocationManager alloc] init];
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        [locationManager startUpdatingLocation];
        CLLocation *location = [locationManager location];

        CLLocationCoordinate2D coord;
        coord.latitude = [_latitude doubleValue];
        coord.longitude = [_longitude doubleValue];

        CLLocation *anotLocation = [[CLLocation alloc] initWithLatitude:coord.latitude longitude:coord.longitude];
        _distancia = [NSNumber numberWithFloat:([location distanceFromLocation:anotLocation]/1000)];
  • 2
    OK, for one, you don't automatically get a location once your location manager starts updating its location. You need to use the didUpdateLocations: delegate method to know when the location manager has updated the location. I your current code, [locationManager location] is actually probably returning nil. – Lyndsey Scott Jul 29 '14 at 18:37
  • Can you print out the values of each location (both lat and lon values) you get in the LLDB (debugger)? – Legoless Jul 29 '14 at 18:56
  • Print of Xcode Debugger [link](http://www.penaz.com.br/img/buglocation) – Zaia Lorenz Jul 29 '14 at 19:33
  • did that work for you? – hackerinheels Aug 12 '14 at 03:01

1 Answers1

0

check out this singleton implementation of location controller

https://github.com/hackerinheels/locationcontroller

with this just call

    CLLocation *currLocation = [[LocationController sharedLocationInterface]getCurrentLocation];
hackerinheels
  • 1,141
  • 1
  • 6
  • 14