App.m
ViewController *vc = [[ViewController alloc]init];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
self.window.rootViewController = nav;
DITU.h
@interface diTu : NSObject
//创建主键
@property(nonatomic,assign)NSInteger integer;
//地址 经度 纬度
@property(nonatomic,strong)NSString *dizhi;
@property(nonatomic,strong)NSString *jinDu;
@property(nonatomic,strong)NSString *weiDu;
loadData.h
#import#import "diTu.h"
@interface loadData : NSObject
{
sqlite3 *DB;
}
// 单例
+(instancetype)shardData;
// 初始化数据库
-(void)initData;
// 创建数据库表格
-(void)createtable;
// 关闭数据库
-(void)closeDataBase;
// 添加数据
-(void)addData:(diTu *)thaData;
// 删除数据
-(void)deleteData:(NSInteger)theId;
// 修改数据
-(void)changeData:(diTu *)theData;
// 查询数据
-(NSMutableArray *)dataArray;
loadData.m
#import "loadData.h"
static loadData *ld = nil;
@implementation loadData
// 单例
+(instancetype)shardData
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
ld = [[loadData alloc]init];
});
return ld;
}
+(instancetype)allocWithZone:(struct _NSZone *)zone
{
if (!ld)
{
ld = [super allocWithZone:zone];
}
return ld;
}
-(id)copy
{
return self;
}
-(id)mutableCopy
{
return self;
}
// 初始化数据库
-(void)initData
{
// 创建Docmuent目录
NSString *strPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
// 拼接数据库的表名的路径
NSString *strName = [strPath stringByAppendingString:@"/didi.db"];
NSLog(@"++++++++%@",strName);
if (sqlite3_open([strName UTF8String], &DB)==SQLITE_OK)
{
NSLog(@"打开是成功的");
[self createtable];
}else
{
NSLog(@"打开失败");
}
}
// 创建数据库表格
-(void)createtable
{
// 创建sql语句,格式:create table if not exists 表名(主键 id integer primary key,加上所有用到的数据)
const char *sql = "create table if not exists diTu(inTeger integer primary key,dizhi text,weidu text,jingdu text)";
// 预编译指针
sqlite3_stmt *stmt;
// 绑定预编译指针
sqlite3_prepare_v2(DB, sql, -1, &stmt, nil);
// 执行预编译指针
if (sqlite3_step(stmt) == SQLITE_DONE)
{
NSLog(@"表格创建成功");
}else
{
NSLog(@"表格创建失败");
}
}
// 添加数据
-(void)addData:(diTu *)thaData
{
// 格式:insert into 表名 values(null,添加的数据?,?)
const char *sql = "insert into diTu values(null,?,?,?)";
// 预编译指针
sqlite3_stmt *stmt;
// 绑定预编译指针
sqlite3_prepare_v2(DB, sql, -1, &stmt, nil);
// 绑定地址占位符
sqlite3_bind_text(stmt, 1, [thaData.dizhi UTF8String], -1, SQLITE_TRANSIENT);
// 绑定经度占位符
sqlite3_bind_text(stmt, 2, [thaData.jinDu UTF8String], -1, SQLITE_TRANSIENT);
// 绑定纬度占位符
sqlite3_bind_text(stmt, 3, [thaData. weiDu UTF8String], -1, SQLITE_TRANSIENT);
// 执行预编译指针
sqlite3_step(stmt);
// 销毁预编译指针
sqlite3_finalize(stmt);
}
// 删除数据
-(void)deleteData:(NSInteger)theId
{
// 格式:delete from 表名 where 表格的主键名:id = ?
const char *sql = "delete from diTu where integer = ?";
// 预编译指针
sqlite3_stmt *stmt;
// 绑定预编译指针
sqlite3_prepare_v2(DB, sql, -1, &stmt, nil);
// 绑定占位符
sqlite3_bind_int(stmt, 1, (int)theId);
// 执行预编译指针
sqlite3_step(stmt);
// 销毁预编译指针
sqlite3_finalize(stmt);
}
// 修改数据
-(void)changeData:(diTu *)theData
{
// 格式:update 表名 set 数据类型 where 主键id
const char *sql = "update diTu set dizhi = ?,weidu = ?,jingdu = ?, where integer = ?";
// 预编译指针
sqlite3_stmt *stmt;
// 绑定预编译指针
sqlite3_prepare_v2(DB, sql, -1, &stmt, nil);
// 绑定占位符
sqlite3_bind_text(stmt, 1, [theData.dizhi UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 2, [theData.jinDu UTF8String], -1, SQLITE_TRANSIENT);
// 绑定占位符
sqlite3_bind_text(stmt, 3, [theData.weiDu UTF8String], -1, SQLITE_TRANSIENT);
// 绑定主键integer
sqlite3_bind_int(stmt, 3, (int)theData.integer);
// 执行预编译指针
sqlite3_step(stmt);
// 销毁预编译指针
sqlite3_finalize(stmt);
}
// 查询数据
-(NSMutableArray *)dataArray
{
// 格式:select *from 表名
const char *sql = "select *from diTu";
// 预编译指针
sqlite3_stmt *stmt;
// 绑定预编译指针
sqlite3_prepare_v2(DB, sql, -1, &stmt, nil);
NSMutableArray *arr = [NSMutableArray array];
while (sqlite3_step(stmt) == SQLITE_ROW)
{
diTu *room = [[diTu alloc]init];
room.integer = sqlite3_column_int(stmt, 0);
room.dizhi = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)];
room.jinDu = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 2)];
room.weiDu = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 3)];
[arr addObject:room];
}
// 销毁预编译指针
sqlite3_finalize(stmt);
return arr;
}
// 关闭数据库
-(void)closeDataBase
{
sqlite3_close(DB);
}
diTuView.h
#import@interface diTuview : UIView
@property(nonatomic,strong)UITextField *dizhiTf;
@property(nonatomic,strong)UITextField *jingduTf;
@property(nonatomic,strong)UITextField *weiduTf;
diTuView.m
-(instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
[self addSubview:self.dizhiTf];
[self addSubview:self.jingduTf];
[self addSubview:self.weiduTf];
}
return self;
}
-(UITextField *)dizhiTf
{
if (!_dizhiTf)
{
_dizhiTf = [[UITextField alloc]initWithFrame:CGRectMake(30, 100, 200, 44)];
_dizhiTf.placeholder = @"请输入地址";
_dizhiTf.borderStyle = UITextBorderStyleRoundedRect;
}
return _dizhiTf;
}
-(UITextField *)weiduTf
{
if (!_weiduTf)
{
_weiduTf = [[UITextField alloc]initWithFrame:CGRectMake(30, 160, 200, 44)];
_weiduTf.placeholder = @"请输入纬度";
_weiduTf.borderStyle = UITextBorderStyleRoundedRect;
_weiduTf.keyboardType = UIKeyboardTypeNumberPad;
}
return _weiduTf;
}
-(UITextField *)jingduTf
{
if (!_jingduTf)
{
_jingduTf = [[UITextField alloc]initWithFrame:CGRectMake(30, 210, 200, 44)];
_jingduTf.placeholder = @"请输入经度";
_jingduTf.borderStyle = UITextBorderStyleRoundedRect;
_jingduTf.keyboardType = UIKeyboardTypeNumberPad;
}
return _jingduTf;
}
mapView.h
#import#import@interface mapView : UIView
@property(nonatomic,strong)UILabel *lab1;
@property(nonatomic,strong)UILabel *lab2;
@property(nonatomic,strong)UITextField *weidu;
@property(nonatomic,strong)UITextField *jingdu;
@property(nonatomic,strong)MKMapView *mapview;
mapView.m
-(instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
[self addSubview:self.mapview];
[self addSubview:self.lab1];
[self addSubview:self.lab2];
[self addSubview:self.jingdu];
[self addSubview:self.weidu];
}
return self;
}
- (MKMapView *)mapview{
if (_mapview == nil)
{
_mapview = [[MKMapView alloc]initWithFrame:self.frame];
_mapview.mapType = MKMapTypeStandard;
_mapview.zoomEnabled = YES;
_mapview.scrollEnabled = YES;
_mapview.rotateEnabled = YES;
_mapview.showsUserLocation = YES;
}
return _mapview;
}
-(UILabel *)lab1
{
if (!_lab1)
{
_lab1 = [[UILabel alloc]initWithFrame:CGRectMake(10,60, 50, 44)];
_lab1.text = @"纬度:";
}
return _lab1;
}
-(UILabel *)lab2
{
if (!_lab2)
{
_lab2 = [[UILabel alloc]initWithFrame:CGRectMake(200,60, 50, 44)];
_lab2.text = @"经度:";
}
return _lab2;
}
-(UITextField *)weidu
{
if (!_weidu)
{
_weidu = [[UITextField alloc]initWithFrame:CGRectMake(50, 60, 150, 44)];
_weidu.placeholder = @"请输入纬度";
_weidu.borderStyle = UITextBorderStyleRoundedRect;
_weidu.keyboardType = UIKeyboardTypeNumberPad;
}
return _weidu;
}
-(UITextField *)jingdu
{
if (!_jingdu)
{
_jingdu = [[UITextField alloc]initWithFrame:CGRectMake(260, 60, 150, 44)];
_jingdu.placeholder = @"请输入经度";
_jingdu.borderStyle = UITextBorderStyleRoundedRect;
_jingdu.keyboardType = UIKeyboardTypeNumberPad;
}
return _jingdu;
}
Viewcon.m
@interface ViewController ()
{
NSMutableArray *array;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[loadData shardData]initData];
[[loadData shardData]createtable];
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(click)];
// 初始化数组
array = [NSMutableArray new];
}
// 导航条右按钮点击方法
-(void)click
{
myViewController *my = [[myViewController alloc]init];
[self.navigationController pushViewController:my animated:YES];
}
// 表格
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 100;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return array.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@""];
if (!cell)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
}
diTu *room = array[indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"%ld\n%@\n%@\n%@",room.integer,room.dizhi,room.weiDu,room.jinDu];
cell.textLabel.numberOfLines = 0;
return cell;
}
// 视图将要出现的方法
-(void)viewWillAppear:(BOOL)animated
{
// 初始化数据库
[[loadData shardData]initData];
// 将查询出来的数据赋值给数组
array = [[loadData shardData]dataArray];
// 关闭数据库
[[loadData shardData]closeDataBase];
// 刷新表格
[self.tableView reloadData];
}
// 删除数据库
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// 初始化数据库
[[loadData shardData]initData];
// 调用数据库里面的删除方法,根据数组下标 找到每一行的主键值
[[loadData shardData]deleteData:[array[indexPath.row]integer]];
// 关闭数据库
[[loadData shardData]closeDataBase];
// 删除单元格内容
[array removeObject:array[indexPath.row]];
// 刷新表格
[self.tableView reloadData];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
mapViewController *mapcom = [[mapViewController alloc]init];
mapcom.map = array[indexPath.row];
[self.navigationController pushViewController:mapcom animated:YES];
}
mapViewController.h
@property(nonatomic,strong)diTu *dt;
mapViewController.m
@interface myViewController ()
{
diTuview *dtview;
}
@end
@implementation myViewController
- (void)viewDidLoad {
[super viewDidLoad];
dtview = [[diTuview alloc]initWithFrame:self.view.frame];
dtview.backgroundColor = [UIColor whiteColor];
self.view = dtview;
dtview.dizhiTf.text = self.dt.dizhi;
dtview.weiduTf.text = self.dt.weiDu;
dtview.jingduTf.text = self.dt.jinDu;
if (dtview.dizhiTf.text.length<=0)
{
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(save)];
}else
{
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(edit)];
}
}
-(void)save
{
diTu *room = [[diTu alloc]init];
room.dizhi = dtview.dizhiTf.text;
room.weiDu = dtview.weiduTf.text;
room.jinDu = dtview.jingduTf.text;
[[loadData shardData]initData];
[[loadData shardData]addData:room];
[[loadData shardData]closeDataBase];
[self.navigationController popViewControllerAnimated:YES];
}
-(void)edit
{
self.dt.dizhi = dtview.dizhiTf.text;
self.dt.weiDu = dtview.weiduTf.text;
self.dt.jinDu = dtview.jingduTf.text;
// 初始化数据库
[[loadData shardData]initData];
// 调用数据库修改方法
[[loadData shardData]changeData:self.dt];
// 关闭数据库
[[loadData shardData]closeDataBase];
[self.navigationController popViewControllerAnimated:YES];
}
mapViewCon.h
@property(nonatomic,strong)diTu *map;
mapViewCon.m
#import "mapViewController.h"#import#import#import "mapView.h"
@interface mapViewController ()
{
mapView *mapp;
}
@end
@implementation mapViewController
- (void)viewDidLoad {
[super viewDidLoad];
mapp = [[mapView alloc]initWithFrame:self.view.frame];
mapp.backgroundColor = [UIColor whiteColor];
self.view = mapp;
mapp.weidu.text = self.map.weiDu;
mapp.jingdu.text = self.map.jinDu;
NSLog(@"+++++___%@",mapp.weidu.text);
NSString *wei = mapp.weidu.text;
NSString *jin = mapp.jingdu.text;
CLLocationCoordinate2D center = {wei.intValue,jin.intValue};
// 设置地图显示的范围,
MKCoordinateSpan span;
// 地图显示范围越小,细节越清楚
span.latitudeDelta = 0.25;
span.longitudeDelta = 0.25;
// 创建MKCoordinateRegion对象,该对象代表了地图的显示中心和显示范围。
MKCoordinateRegion region = {center,span};
// 设置当前地图的显示中心和显示范围
[mapp.mapview setRegion:region animated:YES];
}