業(yè)務(wù)處理層.h
//分享單例對(duì)象
+ (instancetype)shareLoadData;
//獲取數(shù)據(jù)
- (void)getData:(NSDictionary *)dic;
//定義block傳值
@property (nonatomic,strong)void (^dataDic)(NSDictionary *dataDictionary);
.m
//定義靜態(tài)變量
static LoadData *ld = nil;
//分享單例對(duì)象
+ (instancetype)shareLoadData
{
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;
}
//獲取數(shù)據(jù)
- (void)getData:(NSDictionary *)dic
{
//創(chuàng)建請(qǐng)求網(wǎng)址
NSURL *url = [NSURL URLWithString:@"http://api.jisuapi.com/illegaladdr/coord"];
//創(chuàng)建請(qǐng)求對(duì)象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//設(shè)置請(qǐng)求方式
request.HTTPMethod = @"POST";
//設(shè)置請(qǐng)求參數(shù)
NSString *str = [NSString stringWithFormat:@"lat=%@&lng=%@&num=%@&range=%@&appkey=%@",dic[@"lat"],dic[@"lng"],dic[@"num"],dic[@"range"],dic[@"appkey"]];
//設(shè)置請(qǐng)求方法體
request.HTTPBody = [str dataUsingEncoding:NSUTF8StringEncoding];
//獲取會(huì)話對(duì)象
NSURLSession *session = [NSURLSession sharedSession];
//請(qǐng)求數(shù)據(jù)
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//解析json數(shù)據(jù)
NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
//block傳值
dispatch_async(dispatch_get_main_queue(), ^{
self.dataDic(dataDic);
});
}];
//開始請(qǐng)求
[task resume];
}
第一個(gè)視圖
.h
#import "LoadData.h"
#import "DetailViewController.h"
#import<MapKit/MapKit.h>
#import<CoreLocation/CoreLocation.h>
<CLLocationManagerDelegate>
{
//定義變量地圖視圖矮固、定位對(duì)象张足、當(dāng)前位置
MKMapView *mv;
CLLocationManager *lm;
CLLocation *loc;
[super viewDidLoad];
//創(chuàng)建導(dǎo)航按鈕
UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:@"附近違章高發(fā)地" style:UIBarButtonItemStylePlain target:self action:@selector(itemClicked)];
self.navigationItem.rightBarButtonItem = item;
//初始化地圖視圖
mv = [[MKMapView alloc]initWithFrame:self.view.bounds];
mv.mapType = MKMapTypeStandard;
mv.zoomEnabled = YES;
mv.rotateEnabled = YES;
mv.scrollEnabled = YES;
mv.showsUserLocation = YES;
[self.view addSubview:mv];
//初始化定位管理器
lm = [[CLLocationManager alloc]init];
[lm requestWhenInUseAuthorization];
//設(shè)置代理
lm.delegate = self;
//設(shè)置精確度
lm.desiredAccuracy = kCLLocationAccuracyBest;
//色織過(guò)濾器
lm.distanceFilter = kCLDistanceFilterNone;
//開始定位
[lm startUpdatingLocation];
}
//設(shè)置導(dǎo)航按鈕響應(yīng)方法
- (void)itemClicked
{
//跳轉(zhuǎn)
DetailViewController *dvc = [[DetailViewController alloc]init];
dvc.loc = loc;
[self.navigationController pushViewController:dvc animated:YES];
}
//設(shè)置定位響應(yīng)方法- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(nonnull NSArray*)locations
{
loc = locations.lastObject;
//定位位置
MKCoordinateSpan span = {0.01,0.01};
MKCoordinateRegion region = {loc.coordinate,span};
[mv setRegion:region animated:YES];
//添加錨點(diǎn)
MKPointAnnotation *anno = [[MKPointAnnotation alloc]init];
anno.coordinate = loc.coordinate;
[mv addAnnotation:anno];
}
跳轉(zhuǎn)
.h
//定義屬性當(dāng)前位置
@property (nonatomic,strong)CLLocation *loc;
.h
<UITableViewDataSource,UITableViewDelegate>
{
//定義變量數(shù)據(jù)字典碴倾、表格
NSArray *arr;
UITableView *table;
}
//設(shè)置請(qǐng)求參數(shù)
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%f",self.loc.coordinate.latitude],@"lat",[NSString stringWithFormat:@"%f",self.loc.coordinate.longitude],@"lng",@"1000",@"range",@"10",@"num",@"de394933e1a3e2db",@"appkey", nil];
//調(diào)用業(yè)務(wù)處理類請(qǐng)求數(shù)據(jù)
[[LoadData shareLoadData]getData:dic];
//初始化表格
table = [[UITableView alloc]initWithFrame:self.view.bounds];
table.dataSource = self;
table.delegate = self;
[self.view addSubview:table];
- (void)viewWillAppear:(BOOL)animated
{
//更新數(shù)據(jù)字典
[LoadData shareLoadData].dataDic = ^(NSDictionary *dataDictionary) {
arr = dataDictionary[@"result"];
//更新表格
[table reloadData];
};
}
//設(shè)置行數(shù)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return arr.count;
}
//設(shè)置單元格內(nèi)容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellid = @"cellid";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid];
}
NSDictionary *dic = arr[indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"%@%@%@",dic[@"province"],dic[@"city"],dic[@"address"]];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",dic[@"content"]];
return cell;
}