制作地图坐标(lan,& long)存储在sqlite数据库中

发布于 2024-10-31 10:47:51 字数 179 浏览 0 评论 0原文

如何创建一个应用程序,在 SQLite 数据库中记录一系列经度和纬度值,并将它们显示为 MapActivity 上的彩色轨迹。

我需要帮助。如何将地图坐标存储在sqlite数据库中并在表中显示旅程详细信息。例如,我完成了从孟买到浦那的一次旅程。然后如何将数据存储到数据库中以供将来参考。当用户单击旅程名称时它应该提供所有详细信息

how to created an application that records a series of longitude and latitude values in a SQLite database and display them as a coloured track on a MapActivity.

I need help. How can store the map coordinates in sqlite database and display like journey details in table .For example I have done one journey from mumbai to Pune .Then how can store the data into database that can available for future reference .when user click on journey name it should give all details

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

请你别敷衍 2024-11-07 10:47:51

如果您是 Sqlite 新手,那么请查看此类的数据库
创建两个数据库文件如下

---->>>>
Database.h

在此文件中写入以下代码

#import <Foundation/Foundation.h>
#import <sqlite3.h>


@interface DataBase : NSObject {

    sqlite3 *database;

}

+(DataBase *) shareDataBase;

-(BOOL) createDataBase:(NSString *)DataBaseName;

-(NSString*) GetDatabasePath:(NSString *)database;

-(NSMutableArray *) getAllDataForQuery:(NSString *)sql  forDatabase:(NSString *)database;
-(void) inseryQuery:(NSString *) insertSql forDatabase:(NSString *)database1;
-(void) deleteQuery:(NSString *) deleteSql forDatabase:(NSString *)database1;
-(void) updateQuery:(NSString *) updateSql forDatabase:(NSString *)database1;

@end

---->>>>>>
Database.m

在此文件中写入以下代码

#import "DataBase.h"


@implementation DataBase

static DataBase *SampleDataBase =nil;


+(DataBase*) shareDataBase{

    if(!SampleDataBase){
        SampleDataBase = [[DataBase alloc] init];
    }

    return SampleDataBase;

}


-(NSString *) GetDatabasePath:(NSString *)database1{


    [self createDataBase:database1];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:database1];
}


-(BOOL) createDataBase:(NSString *)DataBaseName{
    BOOL success; 

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:DataBaseName];

    success = [fileManager fileExistsAtPath:writableDBPath];
    if (success) return success;
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:DataBaseName];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];

    if (!success) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!!!" message:@"Failed to create writable database" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
        [alert show];
        [alert release];

    }
    return success;
}



-(NSMutableArray *) getAllDataForQuery:(NSString *)sql  forDatabase:(NSString *)database1{

    sqlite3_stmt *statement = nil ;

    NSString *path = [self GetDatabasePath:database1];

    NSMutableArray *alldata;
    alldata = [[NSMutableArray alloc] init];

    if(sqlite3_open([path UTF8String],&database) == SQLITE_OK )
    {
        NSString *query = sql;

        if((sqlite3_prepare_v2(database,[query UTF8String],-1, &statement, NULL)) == SQLITE_OK)
        {
            while(sqlite3_step(statement) == SQLITE_ROW)
            {   

                NSMutableDictionary *currentRow = [[NSMutableDictionary alloc] init];

                int count = sqlite3_column_count(statement);

                for (int i=0; i < count; i++) {

                    char *name = (char*) sqlite3_column_name(statement, i);
                    char *data = (char*) sqlite3_column_text(statement, i);

                    NSString *columnData;
                    NSString *columnName = [NSString stringWithCString:name encoding:NSUTF8StringEncoding];


                    if(data != nil)
                        columnData = [NSString stringWithCString:data encoding:NSUTF8StringEncoding];
                    else {
                        columnData = @"";
                    }

                    [currentRow setObject:columnData forKey:columnName];
                }

                [alldata addObject:currentRow];
            }
        }
        sqlite3_finalize(statement); 
    }
    sqlite3_close(database);

    return alldata;

}

-(void) inseryQuery:(NSString *) insertSql forDatabase:(NSString *)database1{

    sqlite3_stmt *statement = nil ;

    NSString *path = [self GetDatabasePath:database1];

    if(sqlite3_open([path UTF8String],&database) == SQLITE_OK )
    {
        if((sqlite3_prepare_v2(database,[insertSql UTF8String],-1, &statement, NULL)) == SQLITE_OK)
        {
            if(sqlite3_step(statement) == SQLITE_OK){
            }
        }
        sqlite3_finalize(statement); 
    }
    sqlite3_close(database);

}

-(void) updateQuery:(NSString *) updateSql forDatabase:(NSString *)database1{

    sqlite3_stmt *statement = nil ;

    NSString *path = [self GetDatabasePath:database1];

    if(sqlite3_open([path UTF8String],&database) == SQLITE_OK )
    {
        if((sqlite3_prepare_v2(database,[updateSql UTF8String],-1, &statement, NULL)) == SQLITE_OK)
        {
            if(sqlite3_step(statement) == SQLITE_OK){
            }
        }
        sqlite3_finalize(statement); 
    }
    sqlite3_close(database);

}

-(void) deleteQuery:(NSString *) deleteSql forDatabase:(NSString *)database1{

    sqlite3_stmt *statement = nil ;

    NSString *path = [self GetDatabasePath:database1];

    if(sqlite3_open([path UTF8String],&database) == SQLITE_OK )
    {
        if((sqlite3_prepare_v2(database,[deleteSql UTF8String],-1, &statement, NULL)) == SQLITE_OK)
        {
            if(sqlite3_step(statement) == SQLITE_OK){
            }
        }
        sqlite3_finalize(statement); 
    }
    sqlite3_close(database);

}



@end

现在使用以下代码获取数据

NSString *sql = @"select * from UserInfo"; <br>
userInfo = [[DataBase shareDataBase] getAllDataForQuery:sql forDatabase:@"Sample.db"];

它将以 NSDictionary 的形式返回所有行的数组。

要添加新记录,请使用以下代码

NSString *sql = [NSString stringWithFormat:@"insert into userInfo values ('city','name','phone')"];
[[DataBase shareDataBase] inseryQuery:sql forDatabase:@"Sample.db"];

以同样的方式,还有更新和删除记录的方法。

所以这是我见过的最好的例子,我们只需要调用一种方法来获取、插入、更新或删除。

感谢您看到问题,

If you are new to Sqlite Then look into this class for data base
Create two database file as the following

---->>>>
Database.h

Write the following code in this file

#import <Foundation/Foundation.h>
#import <sqlite3.h>


@interface DataBase : NSObject {

    sqlite3 *database;

}

+(DataBase *) shareDataBase;

-(BOOL) createDataBase:(NSString *)DataBaseName;

-(NSString*) GetDatabasePath:(NSString *)database;

-(NSMutableArray *) getAllDataForQuery:(NSString *)sql  forDatabase:(NSString *)database;
-(void) inseryQuery:(NSString *) insertSql forDatabase:(NSString *)database1;
-(void) deleteQuery:(NSString *) deleteSql forDatabase:(NSString *)database1;
-(void) updateQuery:(NSString *) updateSql forDatabase:(NSString *)database1;

@end

---->>>>
Database.m

Write the following code in this file

#import "DataBase.h"


@implementation DataBase

static DataBase *SampleDataBase =nil;


+(DataBase*) shareDataBase{

    if(!SampleDataBase){
        SampleDataBase = [[DataBase alloc] init];
    }

    return SampleDataBase;

}


-(NSString *) GetDatabasePath:(NSString *)database1{


    [self createDataBase:database1];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:database1];
}


-(BOOL) createDataBase:(NSString *)DataBaseName{
    BOOL success; 

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:DataBaseName];

    success = [fileManager fileExistsAtPath:writableDBPath];
    if (success) return success;
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:DataBaseName];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];

    if (!success) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!!!" message:@"Failed to create writable database" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
        [alert show];
        [alert release];

    }
    return success;
}



-(NSMutableArray *) getAllDataForQuery:(NSString *)sql  forDatabase:(NSString *)database1{

    sqlite3_stmt *statement = nil ;

    NSString *path = [self GetDatabasePath:database1];

    NSMutableArray *alldata;
    alldata = [[NSMutableArray alloc] init];

    if(sqlite3_open([path UTF8String],&database) == SQLITE_OK )
    {
        NSString *query = sql;

        if((sqlite3_prepare_v2(database,[query UTF8String],-1, &statement, NULL)) == SQLITE_OK)
        {
            while(sqlite3_step(statement) == SQLITE_ROW)
            {   

                NSMutableDictionary *currentRow = [[NSMutableDictionary alloc] init];

                int count = sqlite3_column_count(statement);

                for (int i=0; i < count; i++) {

                    char *name = (char*) sqlite3_column_name(statement, i);
                    char *data = (char*) sqlite3_column_text(statement, i);

                    NSString *columnData;
                    NSString *columnName = [NSString stringWithCString:name encoding:NSUTF8StringEncoding];


                    if(data != nil)
                        columnData = [NSString stringWithCString:data encoding:NSUTF8StringEncoding];
                    else {
                        columnData = @"";
                    }

                    [currentRow setObject:columnData forKey:columnName];
                }

                [alldata addObject:currentRow];
            }
        }
        sqlite3_finalize(statement); 
    }
    sqlite3_close(database);

    return alldata;

}

-(void) inseryQuery:(NSString *) insertSql forDatabase:(NSString *)database1{

    sqlite3_stmt *statement = nil ;

    NSString *path = [self GetDatabasePath:database1];

    if(sqlite3_open([path UTF8String],&database) == SQLITE_OK )
    {
        if((sqlite3_prepare_v2(database,[insertSql UTF8String],-1, &statement, NULL)) == SQLITE_OK)
        {
            if(sqlite3_step(statement) == SQLITE_OK){
            }
        }
        sqlite3_finalize(statement); 
    }
    sqlite3_close(database);

}

-(void) updateQuery:(NSString *) updateSql forDatabase:(NSString *)database1{

    sqlite3_stmt *statement = nil ;

    NSString *path = [self GetDatabasePath:database1];

    if(sqlite3_open([path UTF8String],&database) == SQLITE_OK )
    {
        if((sqlite3_prepare_v2(database,[updateSql UTF8String],-1, &statement, NULL)) == SQLITE_OK)
        {
            if(sqlite3_step(statement) == SQLITE_OK){
            }
        }
        sqlite3_finalize(statement); 
    }
    sqlite3_close(database);

}

-(void) deleteQuery:(NSString *) deleteSql forDatabase:(NSString *)database1{

    sqlite3_stmt *statement = nil ;

    NSString *path = [self GetDatabasePath:database1];

    if(sqlite3_open([path UTF8String],&database) == SQLITE_OK )
    {
        if((sqlite3_prepare_v2(database,[deleteSql UTF8String],-1, &statement, NULL)) == SQLITE_OK)
        {
            if(sqlite3_step(statement) == SQLITE_OK){
            }
        }
        sqlite3_finalize(statement); 
    }
    sqlite3_close(database);

}



@end

Now to get data use the following code

NSString *sql = @"select * from UserInfo"; <br>
userInfo = [[DataBase shareDataBase] getAllDataForQuery:sql forDatabase:@"Sample.db"];

It will return array of all the row in form of NSDictionary.

To add new record use the following code

NSString *sql = [NSString stringWithFormat:@"insert into userInfo values ('city','name','phone')"];
[[DataBase shareDataBase] inseryQuery:sql forDatabase:@"Sample.db"];

In the same way there is also method to update and delete record.

so This is the best example I have seen we just need to call one method to for fetch, insert , update or delete.

Thanks for seeing the question,

凉宸 2024-11-07 10:47:51

要获取位置,请在项目中导入 corelocation 框架。
请点击此链接

http://developer.apple.com/library/ios/#samplecode/LocateMe/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007801 获取获取位置的示例。

这是在 json 中设置位置的格式

[{"Longitude":"45.2655","Latitude":"23.2655"},{"Longitude":"45.2655","Latitude":"23.2655"},{"Longitude" :"45.2655","纬度":"23.2655"}]

谢谢。

To get location import corelocation framework in your project.
follow this link

http://developer.apple.com/library/ios/#samplecode/LocateMe/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007801 to get sample for getting location.

This is the format to set location in json

[{"Longitude":"45.2655","Latitude":"23.2655"},{"Longitude":"45.2655","Latitude":"23.2655"},{"Longitude":"45.2655","Latitude":"23.2655"}]

Thanks.

北城半夏 2024-11-07 10:47:51

您需要创建一个包含字段......Source,Destination,SourceLat,SourceLong,DestinationLat,DestinationLong.......的表,在此您将通过

Source - Mumbai,(or other) - text - varchar type
Destinatino - Pune, (or other) -text - varchar type
SourceLat - coordinate.latitude; - number with decimal precison upto 10 points.
SourceLong - coordinate.longitude - number with decimal precison upto 10 points.
DestinationLat - coordinate.latitude; - number with decimal precison upto 10 points.
DestinationLong - coordinate.longitude - number with decimal precison upto 10 points.

谢谢,

You need to create a table with fields ...... Source,Destination,SourceLat,SourceLong,DestinationLat,DestinationLong....... and in this you will pass

Source - Mumbai,(or other) - text - varchar type
Destinatino - Pune, (or other) -text - varchar type
SourceLat - coordinate.latitude; - number with decimal precison upto 10 points.
SourceLong - coordinate.longitude - number with decimal precison upto 10 points.
DestinationLat - coordinate.latitude; - number with decimal precison upto 10 points.
DestinationLong - coordinate.longitude - number with decimal precison upto 10 points.

Thanks,

£烟消云散 2024-11-07 10:47:51

首先你需要创建 NSMutableArray *arrayOflocation 对象;在 .h 文件中,

然后在 locationUpdate 方法中编写以下代码,

NSMutableDictionary *LocationDic = [[NSMutableDictionary alloc] init];
[LocationDic setObject:[NSString stringWithFormat:@"%f",c.latitude] forKey:@"Latitude"];
[LocationDic setObject:[NSString stringWithFormat:@"%f",c.longitude] forKey:@"Longitude"];

[arrayOflocation addObject:LocationDic];

现在当您保存行程时,您需要创建 json 格式的字符串,因为您需要使用 json API,您可以使用 google 轻松获取它。当你想将字符串保存到文件中时,编写以下代码。

NSString *dataString = [arrayOflocation JSONRepresentation];
//// code to write dataString in txt file.

最后需要将文件名以及行程的其他详细信息存储在 sqlite 中。

First you need to create object of NSMutableArray *arrayOflocation; in .h file,

Then in you locationUpdate method write the following code

NSMutableDictionary *LocationDic = [[NSMutableDictionary alloc] init];
[LocationDic setObject:[NSString stringWithFormat:@"%f",c.latitude] forKey:@"Latitude"];
[LocationDic setObject:[NSString stringWithFormat:@"%f",c.longitude] forKey:@"Longitude"];

[arrayOflocation addObject:LocationDic];

now when you save the trip you need to create the string for the json format for that you need to use json API you can get it using google easily . write the following code when you want to save the string in file.

NSString *dataString = [arrayOflocation JSONRepresentation];
//// code to write dataString in txt file.

and at finally need to store the file name in sqlite along with other detail for the trip.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文