永远不会调用didUpdateToLocation

时间:2013-07-23 10:15:45

标签: ios objective-c

我想将有关用户位置的信息传递给使用它的另一个函数。委托类MyLocation是存储此信息的单例。

但在我的代码中,didUpdateToLocation永远不会被调用。即使设备静止,每当调用startUpdatingLocation时,是否应至少调用一次?

我确实检查了locationmanager.location.coordinate.latitudelocationmanager.location.coordinate.longitude是否具有正确的值,而且确实如此。已启用位置服务,并且还授予用户访问位置服务的权限。我仍在为iOS 5构建。以前给出的解决方案似乎都不适合我!

有人可以告诉我为什么它不起作用吗?

代码如下:

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 个答案:

没有答案