OC简单地图与网络数据解析

今天我们要做的是一个地图和网络数据解析的工程
首先打开cocopods,导入
pod ‘YYModel’
pod ‘AFNetworking’

我用的MVC的设计模式
首先创建三个文件夹

这里我就把我自己创建的类给大家说一声

在Model里面创建
Model.h
Model.m

然后在View文件夹创建
MYTableViewCell.m
MYTableViewCell.h
这需要勾选上一个xib文件
我们直接在里面创建两个label方便我们的写入

然后Controller里面需要创建一个
NextViewController.m
NextViewController.h
和我们自带的ViewController

好了,创建完成之后,我们开始写代码

首先是Viewcontroller里面的

#import "ViewController.h"
//引入头文件 '地图框架'
#import <MapKit/MapKit.h>
//引入头文件 ‘定位框架’
#import <CoreLocation/CoreLocation.h>

#import "NextViewController.h"

@interface ViewController ()<MKMapViewDelegate,CLLocationManagerDelegate>//地图视图协议
//定义属性 ‘地图视图’
@property(nonatomic,strong)MKMapView *MapView;
//定义属性 ‘地理位置管理者’
@property(nonatomic,strong)CLLocationManager *locManager;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //导航标题  显示当前省会
    self.navigationItem.title = @"违章查询";
    //初始化MKMapView ‘地图视图’
    self.MapView = [[MKMapView alloc]initWithFrame:self.view.frame];
    //设置地图的样式  'MKMapTypeSatellite卫星地图' ‘MKMapTypeStandard平面地图’
    self.MapView.mapType = MKMapTypeStandard;
    //签订协议
    self.MapView.delegate = self;
    //添加到视图
    [self.view addSubview:self.MapView];
    
    //实例化位置管理者对象
    self.locManager = [[CLLocationManager alloc]init];
    self.locManager.delegate = self;
    //申请用户授权
    [self.locManager requestWhenInUseAuthorization];
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"定位" style:UIBarButtonItemStyleDone target:self action:@selector(currentBtnDidPress:)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"查询" style:UIBarButtonItemStyleDone target:self action:@selector(right)];

}
//获取当前按钮触发
-(void)currentBtnDidPress:(id)sender{
    //开始获取位置信息,调用此方法后,协议中的方法才会执行
    [self.locManager startUpdatingLocation];//通过位置管理者开始位置更新
}

//获取当前位置信息后触发的回调方法
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    //获取当前位置  CLLocation位置   locations位置信息一个数组,获取最后一个最新的位置信息
    CLLocation *cation = [locations lastObject];
    //获取经纬度    coordinate坐标  ‘经纬度=当前位置的坐标’
    CLLocationCoordinate2D locationCoor = cation.coordinate;
    //输出一下位置信息   longitude经度   latitude纬度
    NSLog(@"位置:经度:%g,纬度:%g",locationCoor.longitude,locationCoor.latitude);
    
    
    //让地图的显示区缩小   MKCoordinateRegion显示区    传入一个经纬度 和两个需要缩成的大小
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(locationCoor, 180, 180);
    //添加到地图视图上  给地图缩小
    [self.MapView setRegion:region animated:YES];
    
    
    //反向地址解析,将经纬度转换为字符串
    //初始化地址解析  ‘CLGeocoder地址解析’
    CLGeocoder *geocoder = [[CLGeocoder alloc]init];
    //解析   reverseGeocodeLocation传入一个位置
    [geocoder reverseGeocodeLocation:cation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        //获取其中的一个地址信息
        CLPlacemark *place = [placemarks lastObject];
        
        //做一个大头针
        //初始化一个描点
        MKPointAnnotation *point = [[MKPointAnnotation alloc]init];
        //设置大头针的标题   显示当前的位置信息
        point.title = [NSString stringWithFormat:@"我的位置:%@",place.name];
        //设置大头针显示的经纬度  传入locationCoor当前的经纬度
        point.coordinate = locationCoor;
        //添加到地图上   必须是Annotation的类型
        [self.MapView addAnnotation:point];
        
    }];
}
//设置锚点视图的回调方法,当地图调用addAnnotation:方法时会触发   点击大头针是触发的方法
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    //静态字符串标识
    static NSString *iden = @"pin";
    //初始化描点视图
    MKPinAnnotationView *pin = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:iden];
    
    //设置掉落状态
    pin.animatesDrop = YES;
    //设置泡泡效果
    pin.canShowCallout = YES;
    
    return pin;
}

-(void)right{
    NextViewController *next = [NextViewController new];
    [self.navigationController pushViewController:next animated:YES];
}

@end

我直接把我自己的代码拿过来了,具体的我会在里面加注释
需要在Appdelegate里面设置一个

self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:[ViewController new]];

然后是我们创建的NextViewController.m里面

#import "NextViewController.h"
#import "Model.h"
#import <AFNetworking.h>
#import <YYModel.h>
#import "MYTableViewCell.h"

@interface NextViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    Model *mod;
}
@property(nonatomic,strong)UITableView *tbv;
@property(nonatomic,strong)NSMutableArray *DataArr;

@end

@implementation NextViewController

- (UITableView *)tbv{
    if (!_tbv) {
        _tbv = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
        _tbv.delegate = self;
        _tbv.dataSource = self;
        [_tbv registerNib:[UINib nibWithNibName:@"MYTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    }
    return _tbv;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.DataArr = [NSMutableArray array];
    
    [self.view addSubview:self.tbv];
    
    [self loadData];
    
}
//实现网络请求与解析的方法
-(void)loadData{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSDictionary *dic=@{@"lat":@"40.041478",@"lon":@"116.300267",@"key":@"6d96ee265cc090b418ee51257ff9c48f"};
    [manager POST:@"http://v.juhe.cn/wzpoints/query" parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"解析成功");
        NSLog(@"解析的数据=====================%@",responseObject);
        self->mod = [Model yy_modelWithJSON:responseObject];
        NSLog(@"model ============== %@",self->mod);
        [self.tbv reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"解析失败");
    }];
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return mod.result.list.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MYTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    _tbv.rowHeight = 80;
    
    [cell loadDataForModel:[mod.result.list objectAtIndex:indexPath.row]];
      
    return cell;
}

@end

然后是MYTableViewCell.h里面

#import <UIKit/UIKit.h>
#import "Model.h"

NS_ASSUME_NONNULL_BEGIN

@interface MYTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *Lab1;
@property (weak, nonatomic) IBOutlet UILabel *Lab2;

-(void)loadDataForModel:(Model *)mod;

@end

NS_ASSUME_NONNULL_END

这里面的两个属性,Lab1,和Lab2,是我们用xib文件拖进去的

如图所示,这是我们在xib文件里的两个,

然后是MYTableViewCell.m

#import "MYTableViewCell.h"

@implementation MYTableViewCell

- (void)loadDataForModel:(ThreeModel *)mod{
    self.Lab1.text = mod.title;
    self.Lab2.text = [NSString stringWithFormat:@"%@,%@,%@",mod.detail,mod.address,mod.city];
}

@end

最后是Model.m

#import "Model.h"

@implementation ThreeModel


@end

@implementation TwoModel

// 返回容器类中的所需要存放的数据类型 (以 Class 或 Class Name 的形式)。
+ (NSDictionary *)modelContainerPropertyGenericClass {
    return @{@"list" : [ThreeModel class]};
}

@end


@implementation Model

// 返回容器类中的所需要存放的数据类型 (以 Class 或 Class Name 的形式)。
+ (NSDictionary *)modelContainerPropertyGenericClass {
    return @{@"result" : [TwoModel class]};
}

@end

Model.h


#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface ThreeModel : NSObject
@property(nonatomic,strong)NSString *address;
@property(nonatomic,strong)NSString *city;
@property(nonatomic,strong)NSString *detail;
@property(nonatomic,strong)NSString *title;
@end

@interface TwoModel : NSObject
@property(nonatomic,strong)NSArray *list;
@end

@interface Model : NSObject
@property(nonatomic,strong)NSString *reason;
@property(nonatomic,strong)TwoModel *result;
@end

NS_ASSUME_NONNULL_END

到这我们基本就已经完成了

1. 用户权限管理模块 角色管理: 学生:查看实验室信息、预约设备、提交耗材申请、参安全考核 教师:管理课题组预约、审批学生耗材申请、查看本课题组使用记录 管理员:设备全生命周期管理、审核预约、耗材采购分发、安全检查 用户操作: 登录认证:统一身份认证(对接学号 / 工号系统,模拟实现),支持密码重置 信息管理:学生 / 教师维护个人信息(联系方式、所属院系),管理员管理所有用户 权限控制:不同角色仅可见对应功能(如学生不可删除设备信息) 2. 实验室设备管理模块 实验室信息管理: 基础信息:实验室编号、名称、位置、容纳人数、开放时间、负责人 功能分类:按学科(计算机实验室 / 电子实验室 / 化学实验室)标记,关联可开展实验类型 状态展示:实时显示当前使用人数、设备运行状态(正常 / 故障) 设备管理: 设备档案:名称、型号、规格、购置日期、单价、生产厂家、存放位置、责任人 全生命周期管理: 入库登记:管理员录入新设备信息,生成唯一资产编号 维护记录:记录维修、校准、保养信息(时间、内容、执行人) 报废处理:登记报废原因、时间,更新设备状态为 "已报废" 设备查询:支持按名称、型号、状态多条件检索,显示设备当前可用情况 3. 预约使用模块 预约管理: 预约规则:学生可预约未来 7 天内的设备 / 实验室,单次最长 4 小时(可设置) 预约流程:选择实验室→选择设备→选择时间段→提交申请(需填写实验目的) 审核机制:普通实验自动通过,高危实验(如化学实验)需教师审核 使用记录: 签到 / 签退:到达实验室后扫码签到,离开时签退,系统自动记录实际使用时长 使用登记:填写实验内容、设备运行情况(正常 / 异常),异常情况需详细描述 违规管理:迟到 15 分钟自动取消预约,多次违规限制预约权限 4. 耗材安全管理模块 耗材管理: 耗材档案:名称、规格、数量、存放位置、
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值