从数据库中获取纬度和经度的浮点值

时间:2012-12-18 11:27:32

标签: iphone

我试图获取存储在我的数据库中的某些区域的纬度和经度,并将它们存储在一个数组中。我必须将值存储为float类型。我通过以下代码获取其他值:

NSString *walkQuery = [[NSString alloc] initWithFormat:@"SELECT Wid,WName,SName,Latitude,Longitude from Walks,SubRegions WHERE Walks.Sid=SubRegions.Sid AND Rid = %d",[regionId integerValue] ];

        sqlite3_stmt *walkstatement = nil;

        if (sqlite3_prepare_v2(walkNameDB,[walkQuery UTF8String], -1, &walkstatement, nil) == SQLITE_OK)
        {
            while( sqlite3_step(walkstatement) == SQLITE_ROW )
            {
                NSNumber *WId;
                int temp1 = (int)sqlite3_column_int(walkstatement, 0);
                WId = [[NSNumber alloc] initWithInt:temp1];

                char *WNameCharacter;
                WNameCharacter = (char *) sqlite3_column_text(walkstatement, 1);
                NSString *WNameString = [[NSString alloc] initWithUTF8String:WNameCharacter];

                char *SNameCharacter;
                SNameCharacter = (char *) sqlite3_column_text(walkstatement, 2);
                NSString *SNameString = [[NSString alloc] initWithUTF8String:SNameCharacter];


                NSMutableDictionary *tempWalk = [[NSMutableDictionary alloc] init];
                [tempWalk setObject:WId forKey:@"WalkId"];
                [tempWalk setObject:WNameString forKey:@"WalkName"];
                [tempWalk setObject:SNameString forKey:@"SubRegionName"];
                [regionWalkArray addObject:tempWalk];

他们可以通过哪种方式获得纬度和经度的两个浮动值,就像我在我的代码中一样?

请建议......

1 个答案:

答案 0 :(得分:0)

您可以使用:

 float latitude  = (float) sqlite3_column_double(walkstatement, 3);
 float longitude = (float) sqlite3_column_double(walkstatement, 4);

可以存储在字典中:

[tempWalk setObject:[NSNumber numberWithFloat:latitude] forKey:@"latitude"];
[tempWalk setObject:[NSNumber numberWithFloat:longitude] forKey:@"longitude"];

您可以检索它:

float latitude = [[tempWalk objectForKey:@"latitude"] floatValue];
float longitude = [[tempWalk objectForKey:@"longitude"] floatValue];