#import "LoadData.h"
#import "Model.h"
// 创建一个静态变量
static LoadData *ld = nil;
#define URL @"http://api.jisuapi.com/illegaladdr/coord?lat=31.3004088721&lng=121.4849729860&range=1000&num=10&appkey=de394933e1a3e2db"
@implementation LoadData
//单例
+ (instancetype)sharedLoadData{
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)getData:(WebData)block{
NSURL *url = [NSURL URLWithString:URL];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];
request.HTTPMethod = @"POST";
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
block(dic);
}];
[task resume];
}
#import "ViewController.h"
#import "MapView.h"
#import "XQViewController.h"
@interface ViewController ()
@property (nonatomic,strong) MapView *mapView;
@end
@implementation ViewController
- (MapView *)mapView{
if (!_mapView) {
_mapView = [[MapView alloc]initWithFrame:self.view.frame];
}
return _mapView;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view = self.mapView;
// 导航条右侧按钮
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"附近违章高发地" style:UIBarButtonItemStylePlain target:self action:@selector(weiZhang)];
}
- (void)weiZhang{
XQViewController *xq = [[XQViewController alloc]init];
[self.navigationController pushViewController:xq animated:YES];
}
#import "XQViewController.h"
#import "LoadData.h"#import "Model.h"
@interface XQViewController ()@property (nonatomic,strong) UITableView *tableViews;
@property (nonatomic,strong) NSDictionary *dict;
@property (nonatomic,strong) Model *model;
@property (nonatomic,strong) NSArray *resultArray;
@property (nonatomic,strong) NSMutableArray *modelArray;
@end
@implementation XQViewController
// 懒加载控件
- (UITableView *)tableViews{
if (!_tableViews) {
_tableViews = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
[_tableViews setDelegate:self];
[_tableViews setDataSource:self];
[_tableViews setRowHeight:120];
}
return _tableViews;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[[LoadData sharedLoadData]getData:^(NSDictionary *dict) {
self.dict = [NSDictionary dictionaryWithDictionary:dict];
self.resultArray = [NSArray arrayWithArray:self.dict[@"result"]];
self.modelArray = [[NSMutableArray alloc]init];
for (NSDictionary *arrayDic in self.resultArray) {
self.model = [Model modelWithdict:arrayDic];
[self.modelArray addObject:self.model];
}
NSLog(@"%@",self.modelArray);
[self.tableViews reloadData];
}];
[self.view addSubview:self.tableViews];
}
#pragma mark - UITableViewDataSource -
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.modelArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *reuseID = @"cells";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseID];
}
self.model = self.modelArray[indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"address=%@,\ncity=%@,\ncontent=%@,\ndistance=%@,\nlat=%@,\nlng=%@,\nnum=%@,\nprovince=%@,\ntown=%@",self.model.address,self.model.city,self.model.content,self.model.distance,self.model.lat,self.model.lng,self.model.num,self.model.province,self.model.town];
cell.textLabel.numberOfLines = 0;
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import#import@interface MapView : UIView
@property (nonatomic,strong) MKMapView *mapView;
@end
#import "MapView.h"
@implementation MapView
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
[self addSubview:self.mapView];
}
return self;
}
- (MKMapView *)mapView{
if (!_mapView) {
_mapView = [[MKMapView alloc]initWithFrame:self.frame];
[_mapView setMapType:MKMapTypeStandard];
[_mapView setZoomEnabled:YES];
[_mapView setScrollEnabled:YES];
// 设置地图定位
[self locateToLatitude:31.3004088721 longitude:121.4849729860];
}
return _mapView;
}
- (void)locateToLatitude:(CGFloat)latitude longitude:(CGFloat)longitude{
//设置地图中心的经、纬度
CLLocationCoordinate2D center = {latitude , longitude};
//设置地图显示的范围,
MKCoordinateSpan span;
//地图显示范围越小,细节越清楚
span.latitudeDelta=0.01;
span.longitudeDelta=0.01;
//创建MKCoordinateRegion对象,该对象代表了地图的显示中心和显示范围。
MKCoordinateRegion region = {center,span};
//设置当前地图的显示中心和显示范围
[self.mapView setRegion:region animated:YES];
//创建MKPointAnnotation对象——代表一个锚点
MKPointAnnotation* annotation = [[MKPointAnnotation alloc]init];
annotation.title=@"北京石羿科技发展有限公司";
annotation.subtitle=@"海淀区中关村软件园";
CLLocationCoordinate2D coordinate = {latitude , longitude};
annotation.coordinate= coordinate;
//添加锚点
[self.mapView addAnnotation:annotation];
}
#import@interface Model : NSObject
@property (nonatomic,copy) NSString *address;
@property (nonatomic,copy) NSString *city;
@property (nonatomic,copy) NSString *content;
@property (nonatomic,copy) NSString *distance;
@property (nonatomic,copy) NSString *lat;
@property (nonatomic,copy) NSString *lng;
@property (nonatomic,copy) NSString *num;
@property (nonatomic,copy) NSString *province;
@property (nonatomic,copy) NSString *town;
+ (instancetype)modelWithdict:(NSDictionary *)dic;
- (instancetype)initWithDict:(NSDictionary *)dict;
@end
#import "Model.h"
@implementation Model
+ (instancetype)modelWithdict:(NSDictionary *)dic{
return [[self alloc]initWithDict:dic];
}
- (instancetype)initWithDict:(NSDictionary *)dict{
self = [super init];
if (self) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
网友评论