//业务处理层.h
import <Foundation/Foundation.h>
@interface LoadData : NSObject
//分享单例对象
- (instancetype)shareLoadData;
//获取数据
- (void)getData:(NSDictionary *)dic;
//定义block传值
@property (nonatomic,strong)void (^dataDic)(NSDictionary *dataDictionary);
@end
.m文件
import "LoadData.h"
//定义静态变量
static LoadData *ld = nil;
@implementation LoadData
//分享单例对象
- (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;
}
//http://api.jisuapi.com/illegaladdr/coord?lat=31.3004&lng=121.484&range=1000&num=10&appkey=de394933e1a3e2db
//获取数据-
(void)getData:(NSDictionary *)dic
{
//创建请求网址
NSURL *url = [NSURL URLWithString:@"http://api.jisuapi.com/illegaladdr/coord"];
//创建请求对象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//设置请求方式
request.HTTPMethod = @"POST";
//设置请求参数
NSString *str = [NSString stringWithFormat:@"lat=%@&lng=%@&num=%@&range=%@&appkey=%@",dic[@"lat"],dic[@"lng"],dic[@"num"],dic[@"range"],dic[@"appkey"]];
//设置请求方法体
request.HTTPBody = [str dataUsingEncoding:NSUTF8StringEncoding];
//获取会话对象
NSURLSession *session = [NSURLSession sharedSession];
//请求数据
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//解析json数据
NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSLog(@"=============%@",dataDic);
//block传值
dispatch_async(dispatch_get_main_queue(), ^{
self.dataDic(dataDic);
});
}];//开始请求
[task resume];
}
@end
//#import "ViewController.m"
import "ViewController.h"
import "LoadData.h"
import "DetailViewController.h"
import <MapKit/MapKit.h>
import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate>
{
//定义变量地图视图、定位对象、当前位置
MKMapView *mv;
CLLocationManager *lm;
CLLocation *loc;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//创建导航按钮
UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:@"附近违章高发地" 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];
//设置代理
lm.delegate = self;
//设置精确度
lm.desiredAccuracy = kCLLocationAccuracyBest;
//色织过滤器
lm.distanceFilter = kCLDistanceFilterNone;
//开始定位
[lm startUpdatingLocation];
}
//设置导航按钮响应方法
- (void)itemClicked
{
//跳转
DetailViewController *dvc = [[DetailViewController alloc]init];
dvc.loc = loc;
[self.navigationController pushViewController:dvc animated:YES];
}
//设置定位响应方法 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations
{
loc = locations.lastObject;
//定位位置
MKCoordinateSpan span = {0.01,0.01};
MKCoordinateRegion region = {loc.coordinate,span};
[mv setRegion:region animated:YES];
//添加锚点
MKPointAnnotation *anno = [[MKPointAnnotation alloc]init];
anno.coordinate = loc.coordinate;
[mv addAnnotation:anno];
}
//跳转页面.h
import <UIKit/UIKit.h>
import <CoreLocation/CoreLocation.h>
@interface DetailViewController : UIViewController
//定义属性当前位置
@property (nonatomic,strong)CLLocation *loc;
@end
//跳转页面.m
import "DetailViewController.h"
import "LoadData.h"
@interface DetailViewController ()<UITableViewDataSource,UITableViewDelegate>
{
//定义变量数据字典、表格
NSArray *arr;
UITableView *table;
}
@end
@implementation DetailViewController
(void)viewDidLoad {
[super viewDidLoad];
//设置请求参数
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];
//调用业务处理类请求数据
[[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
{//更新数据字典
[LoadData shareLoadData].dataDic = ^(NSDictionary *dataDictionary) {
arr = dataDictionary[@"result"];NSLog(@"-----------%@",arr); //更新表格 [table reloadData];
};
}
//设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return arr.count;
}
//设置单元格内容 - (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;
}