0

問題なく現在地を取得しています。しかし、距離を計算しようとすると、currentLoc の値が 0 になります。その currentLoc を引き継いで 0 にならないようにするには、何が欠けていますか?

LocationManager の NSLog は正しい値を提供します。

#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;
}

私の NSLog が現在の場所の緯度と経度に対して 0 の値を示しているのは、私の配列にあります。

-(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
4

0 に答える 0