1安装cocoaPos
教程超级传送门
这次我们是利用AFNetWoring
地址:https://github.com/AFNetworking/AFNetworking
我们把AFNetWoring文件后安装完如下.
1.注册百度地图的.key.
地址: http://lbsyun.baidu.com/index.php
2.创建界面ui.(简单点好了.)
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UILabel *province = [[UILabel alloc]initWithFrame:CGRectMake(0, 200, 180,80)];
province.text = @"省份";
province.layer.borderWidth = 2.0f;
province.layer.cornerRadius = 5;
province.layer.borderColor =[UIColor blueColor].CGColor;
[self.view addSubview:province];
UILabel *city = [[UILabel alloc]initWithFrame:CGRectMake(200, 200 , 180 ,80)];
city.text = @"城市";
city.layer.borderWidth = 2.0f;
city.layer.cornerRadius = 5;
city.layer.borderColor =[UIColor blueColor].CGColor;
[self.view addSubview:city];
__cityText = [[UITextField alloc]initWithFrame:CGRectMake(0, 300 , 180,80)];
__cityText.layer.borderWidth = 2.0f;
__cityText.layer.cornerRadius = 5;
__cityText.layer.borderColor = [UIColor blueColor].CGColor;
__cityText.text = @"北京";
[self.view addSubview: __cityText];
_addrText = [[UITextField alloc]initWithFrame:CGRectMake(200, 300 ,180 ,80)];
_addrText.layer.borderWidth = 2.0f;
_addrText.layer.cornerRadius = 5;
_addrText.layer.borderColor = [UIColor blueColor].CGColor;
_addrText.text = @"中关村";
[self.view addSubview: _addrText];
_mapXY = [[UILabel alloc]initWithFrame:CGRectMake(0, 100 , self.view.bounds.size.width,80)];
_mapXY.layer.borderWidth = 2.0f;
_mapXY.layer.cornerRadius = 5;
_mapXY.text = @"等待输入";
_mapXY.layer.borderColor = [UIColor lightGrayColor].CGColor;
[self.view addSubview: _mapXY];
_ak = @"你的key&mcode=百度开发者,ios端安全码 ;";
[self getMap];
}
3.get请求.
-(void)getMap{
//地址栏
NSString *str=[NSString stringWithFormat:@"http://api.map.baidu.com/geocoder/v2/?address=%@%@&output=json&ak=%@",__cityText.text,_addrText.text,_ak ];
//转换中文字
NSString *getMapXY = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
//进程
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//请求
[manager GET:getMapXY parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable result) {
NSLog(@"JSON: %@", result);
/*
MapFoApi[4367:342396] JSON: {
result = {
confidence = 80;
level = "\U9053\U8def";
location = {
lat = "40.056890127931";
lng = "116.30815063007";
};
precise = 1;
};
status = 0;
}
*/
//提出啊数据
NSDictionary *dicMap =[result objectForKey:@"result"];
_mapLat = [[ dicMap objectForKey:@"location"]objectForKey:@"lat"];
_mapLng =[[ dicMap objectForKey:@"location"]objectForKey:@"lng"];
_mapXY.text = [NSString stringWithFormat:@"经度:%@,纬度:%@",_mapLng,_mapLat];
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
这里不得说一下,网上基本上都是2.0 版本的AFHTTPRequestOperationManager.
现在 3.0版本下面的类已从AFNetworking中废弃:
AFURLConnectionOperation
AFHTTPRequestOperation
AFHTTPRequestOperationManager
运行一下..
获取app配置信息失败: The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.
什么回事呢?
由于iOS9改用更安全的https,,请在"Info.plist"中进行如下配置,否则影响SDK,API的使用。
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
Snip20160417_3.png
设置运行起来