开发者

making map coordinates(lan,& long)storing in sqlite database

开发者 https://www.devze.com 2023-02-24 15:28 出处:网络
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.

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 stor开发者_运维问答e 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


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,


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.


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,


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号