Ios 丢失当前位置值

Ios 丢失当前位置值,ios,xcode,cllocation,Ios,Xcode,Cllocation,我现在的位置没有问题。但是,当我试图计算距离时,我的currentLoc值为0。我错过了什么来把它带过来,所以它不是0 我的NSLog in LocationManager提供了正确的值 #import "AuthorViewController.h" #import "Author.h" #import <sqlite3.h> @implementation AuthorViewController @synthesize theauthors; @synthesize cur

我现在的位置没有问题。但是,当我试图计算距离时,我的currentLoc值为0。我错过了什么来把它带过来,所以它不是0

我的NSLog in LocationManager提供了正确的值

#import "AuthorViewController.h"
#import "Author.h"
#import <sqlite3.h>


@implementation AuthorViewController
@synthesize theauthors;
@synthesize currentLoc;

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    currentLoc = newLocation;

    if (newLocation.horizontalAccuracy <= 100.0f) {
        [locMgr stopUpdatingLocation];
}
    NSLog(@"Lat: %f, Long: %f", currentLoc.coordinate.latitude, currentLoc.coordinate.longitude);

    [self barList];
    [self.tableView reloadData];
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSString *msg = [[NSString alloc]initWithString:@"Error obtaining location"];
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:msg delegate:nil cancelButtonTitle:@"Done" otherButtonTitles:nil];
    [alert show];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    locMgr = [[CLLocationManager alloc] init];
    locMgr.delegate = self;
    [locMgr startUpdatingLocation];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    return [self.theauthors count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
    int rowCount = indexPath.row;

    Author *author = [self.theauthors objectAtIndex:rowCount];

    cell.textLabel.text = author.barName;

    if (currentLoc == nil) {
        cell.detailTextLabel.text = [NSString stringWithFormat:@"?"];
    }else
    {
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%.02f km", author.cachedDist];
    }


    return cell;
}

-1.我看不到任何联系。您似乎正在从数据库中获取数据,但要插入到哪里?我没有看到在数据库中进行任何插入。此外,您正在两个不同的位置打印纬度和经度。一个位置是属性,另一个来自sql数据库。你怎么能期望他们是一样的?您甚至没有提到文件名。糟糕的问题!对不起,这个问题怎么不好。如果我把所有的答案都更新了,我就不会问问题了。我在两个地方打印,看看发生了什么。我试图使用currentLoc计算距离,并使用[theAuthors addObject:author]将其插入我的数组中;我不想在数据库中插入任何内容。我只是尝试使用DB来更新数组并在我的TableView中显示。也许我确实需要将距离插入DB,然后将其全部拉出,但我不认为我必须这样做。这也是我问这个问题的原因。你需要提供所有的细节。您没有向我们显示在数据库中插入的位置。您没有向我们展示代码的两部分是如何连接的。你有“我得到我的当前位置没有问题。但是当我试图计算我的距离时,我的currentLoc有一个0值。”这是一个怎样的问题?您有两种不同的打印设置。。。您的参考点不正确。数据库是预加载的sql数据库。我的第一个代码显示了我在哪里得到我的当前位置和打印,以确保它的工作。我的第二段代码显示了我从预加载的sql DB中提取信息并将其添加到我的“theauthors”数组的位置。我再次在这里打印,以查看在执行计算时,我的currentLoc是否仍然填充了正确的位置。我没有显示我插入数据库的位置,因为我不是。主要问题是为什么我的currentLoc会将其位置数据从顶部代码丢失到底部代码。我已经更新了完整的代码。在
didUpdateToLocation
中,尝试更改
currentLoc=newLocation
self.currentLoc=newLocation
-(NSMutableArray *) barList{
    theauthors = [[NSMutableArray alloc] init];
    @try {
        NSFileManager *fileMgr = [NSFileManager defaultManager];
        NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"TulsaBars.sqlite"];
        BOOL success = [fileMgr fileExistsAtPath:dbPath];
    if(!success)
    {
        NSLog(@"Cannot locate database file '%@'.", dbPath);
    }
    if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
    {
        NSLog(@"An error has occured: %@", sqlite3_errmsg(db));

    }


    const char *sql = "SELECT * FROM  TulsaBars";
    sqlite3_stmt *sqlStatement;
    if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
    {
        NSLog(@"Problem with prepare statement:  %@", sqlite3_errmsg(db));
    }else{

        while (sqlite3_step(sqlStatement)==SQLITE_ROW) {
            Author * author = [[Author alloc] init];
            author.barName = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,1)];
            author.barAddress = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,2)];
            author.barState = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 5)];
            author.barLat = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 8)];
            author.barLong = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 9)];


            if (currentLoc == nil) {
                NSLog(@"current location is nil %@", currentLoc);
            }

            CLLocation *barLocation = [[CLLocation alloc] initWithLatitude:[author.barLat doubleValue] longitude:[author.barLong doubleValue]];
            CLLocationDistance distancekm = [currentLoc distanceFromLocation: barLocation]/1000;
            NSString *distanceString = [[NSString alloc] initWithFormat: @"%f", distancekm];
            author.cachedDist = distanceString;

            [theauthors addObject:author];

            NSLog(@"Lat3: %@, Long3: %@, CurLocLat: %f, CurLocLong: %f, BarLoc: %@, DistKM: %@", author.barLat, author.barLong, currentLoc.coordinate.latitude, currentLoc.coordinate.longitude, barLocation, distancekm);

        }
    }
    sqlite3_finalize(sqlStatement);
}
@catch (NSException *exception) {
    NSLog(@"Problem with prepare statement:  %@", sqlite3_errmsg(db));
}
@finally {
    sqlite3_close(db);

    return theauthors;
}
}
end