-1

I am building an app which requires me to find the distance between a location specified and my current location. I am using CLLocationManager for the same but the coordinates retrieved are 0,0 despite me specifying a custom location in the simulator. Here's the code: The .m file

@property (nonatomic, strong) CLLocationManager *locationManager2;
...
@synthesize locationManager2;
- (CLLocationManager *)locationManager2 {

if (locationManager2 != nil) {
    return locationManager2;
}

locationManager2 = [[CLLocationManager alloc] init];
[locationManager2 setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
[locationManager2 setDelegate:self];

return locationManager2;
}
 ...
 ...
 ...
  CLLocation *locationHome = [locationManager2 location];

NSLog(@"%f",locationHome.coordinate.latitude);

The lattitude is logged as zero.Anything wrong with my code?

Parth Mody
  • 75
  • 3
  • 8
  • Simulator does not support providing location. You have to use device. – Geek Jun 22 '13 at 06:50
  • 3
    This is not how location manager works. You have to start location services and await a call to the delegate method. You cannot reliably just create a location manager object and inquire its location. This happens asynchronously. See the [Location Awareness Programming Guide](http://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html#//apple_ref/doc/uid/TP40009497-CH2-SW9). – Rob Jun 22 '13 at 06:51
  • I use iOS 6 and gets nil in simulator. As suggested by Rob try this : [locationManager2 startUpdatingLocation]; CLLocation * locationHome = locationManager2.location; [locationManager2 stopUpdatingLocation]; – Geek Jun 22 '13 at 06:59

1 Answers1

0

Put this code and let me know what you are getting at console.

This code built on iOS 6.

CLLocationManager *locationManager = [[CLLocationManager alloc] init];
if ([CLLocationManager locationServicesEnabled])
{
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    [locationManager startUpdatingLocation];
}

location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];;
MKCoordinateRegion region;
region.center=coordinate;
MKCoordinateSpan span;
span.latitudeDelta=10.015;
span.longitudeDelta=10.015;
region.span=span;
[mapView setRegion:region];

NSString *str=[[NSString alloc] initWithFormat:@" latitude:%f longitude:%f",coordinate.latitude,coordinate.longitude];
NSLog(@"%@",str);
Abizern
  • 146,289
  • 39
  • 203
  • 257
Kumar KL
  • 15,315
  • 9
  • 38
  • 60