Search code examples
iphonedelegatescore-locationcllocationmanagercllocation

Core Location returning 00


Here's the full method, I didn't put the second half originally because I know it works:

 -(void)showDirections
{
    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
    [locationManager setDelegate:self];

    CLLocation *location = [locationManager location];

    CLLocationCoordinate2D coordinate = [location coordinate];
    NSNumber *myLatitude = [NSNumber numberWithDouble:coordinate.latitude];
    NSNumber *myLongitude = [NSNumber numberWithDouble:coordinate.longitude];
    double myLatitude2 = [myLatitude doubleValue];
    double myLongitude2 = [myLongitude doubleValue];


    NSArray *array = [dataHold objectForKey:@"Subtree"];
    NSString *latitude = [NSString stringWithFormat:@"%@",[array objectAtIndex:4]];
    NSString *longitude = [NSString stringWithFormat:@"%@",[array objectAtIndex:5]];
    double clubLatitude = [latitude doubleValue];
    double clubLongitude = [longitude doubleValue];
    CLLocationCoordinate2D start = { myLatitude2, myLongitude2};    
    CLLocationCoordinate2D destination = { clubLatitude, clubLongitude};        
    NSString *googleMapsURLString = [NSString stringWithFormat:@"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f",start.latitude, start.longitude, destination.latitude, destination.longitude];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapsURLString]];

}

Solution

  • Two issues:

    1) You are doing a NSNumber box/unbox cycle that has no use whatsoever

    2) Core Location is asynchronous, therefore you cannot return the user's location straight away, you must use the delegate method(s) to retrieve the location if/when it is available.

    Here's an example of how it should work:

     -(void)showDirections
    {
        locationManager = [[CLLocationManager alloc] init];
        [locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
        [locationManager setDelegate:self];
    
        [locationManager startUpdatingLocation];
    }
    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
        CLLocationCoordinate2D coord = [newLocation coordinate];
        NSArray *array = [dataHold objectForKey:@"Subtree"];
        NSString *latitude = [NSString stringWithFormat:@"%@",[array objectAtIndex:4]];
        NSString *longitude = [NSString stringWithFormat:@"%@",[array objectAtIndex:5]];
        double clubLatitude = [latitude doubleValue];
        double clubLongitude = [longitude doubleValue];
        NSString *googleMapsURLString = [NSString stringWithFormat:@"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f", coord.latitude, coord.longitude, clubLatitude, clubLongitude];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapsURLString]];
    }