0

I want to pass information about the location of the user to another function which uses it. The delegate class MyLocation is a singleton which stores this information.

But in my code the didUpdateToLocation never gets called . Should n't it be called at least once whenever startUpdatingLocation is called even if the device is stationary?

I did check if locationmanager.location.coordinate.latitude and locationmanager.location.coordinate.longitude have the right values and they do. The location services are enabled and the user permission to access location services is also granted. I am still building for iOS 5. None of the previously given solutions seem to work for me!

Can someone please give me some idea as to why it is not working?

The code is as follows:

CLLocationManager *locationManager;
CLGeocoder *geocoder;
CLPlacemark *placemark;

@implementation MyLocation {
}

+ (id)getInstance {
    static MyLocation *sharedMyLocation = nil;
    static int i=0;
    if(i==0){
        sharedMyLocation = [[MyLocation alloc] init];
        i=1;
    }
    return sharedMyLocation;
}   

- (id)init {
    if (self = [super init]) {
        latitude = [[NSString alloc] init];
        longitude = [[NSString alloc] init];
        country = [[NSString alloc] init];
        admin_area = [[NSString alloc] init];
        postal_code = [[NSString alloc] init];
        locality = [[NSString alloc] init];
        subtfare = [[NSString alloc] init];
        tfare = [[NSString alloc] init];
    }
    return self;
}

- (void) startupdate{
    if(locationManager == nil)
    {
        locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self;
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    }
    if(geocoder == nil)
       geocoder = [[CLGeocoder alloc] init];


    [locationManager startUpdatingLocation];    
}


- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error  
{
    NSLog(@"didFailWithError: %@", error);
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation        *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

    if(currentLocation != nil){
        longitude = [NSString stringWithFormat:@"%.8f",   currentLocation.coordinate.longitude];
        latitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
    }

    [locationManager stopUpdatingLocation];

    // Reverse Geocoding
    NSLog(@"Resolving the Address");
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
        if (error == nil && [placemarks count] > 0) {
            placemark = [placemarks lastObject];
            country = [NSString stringWithFormat:@"%@",placemark.country];
            admin_area = [NSString stringWithFormat:@"%@",placemark.administrativeArea];
            postal_code = [NSString stringWithFormat:@"%@",placemark.postalCode];
            locality = [NSString stringWithFormat:@"%@",placemark.locality];
            subtfare = [NSString stringWithFormat:@"%@",placemark.subThoroughfare];
            tfare = [NSString stringWithFormat:@"%@",placemark.thoroughfare];
        } else {
            NSLog(@"%@", error.debugDescription);
        }
    } ];
}


- (NSString*)  getLatitude
{
    return latitude;
}


- (NSString*) getLongitude
{
    return longitude;
}

- (NSString*) getCountry
{
    return country;
}

- (NSString*) getAdminArea
{
    return admin_area;
}

- (NSString*) getPostalCode
{
    return postal_code;
}

- (NSString*) getLocality
{
    return locality;
}

- (NSString*) getSubTFare
{
    return subtfare;
} 

- (NSString*) getTFare
{
    return tfare;
}


error GPS_INTERFACE::getLatitude(char* buffer , unsigned int len)
{
    id temp = [MyLocation getInstance];
    [temp startupdate];
    NSString* tempbuf = [temp getLatitude];
    NSString *l = [NSString stringWithFormat:@"%@",tempbuf];
    const char* lati= [l UTF8String];
    if(strlen(lati) < len)
        std::strncpy(buffer,lati,[l length]);
    else 
        return Buffer_Insufficent;

    return No_Error; 
}
/* other similar getter functions! */

0 Answers0