Oc CLGeocoder(地图解析)
来源:互联网 发布:算法c语言实现高清pdf 编辑:程序博客网 时间:2024/06/09 21:40
#import "ViewController.h"#import <CoreLocation/CoreLocation.h>@interface ViewController ()//地址输入框@property (weak, nonatomic) IBOutlet UITextField *addrField;//经度@property (weak, nonatomic) IBOutlet UITextField *longitudeField;//纬度@property (weak, nonatomic) IBOutlet UITextField *latitudeField;//显示内容框@property (weak, nonatomic) IBOutlet UITextView *resultView;//解析地址- (IBAction)encodeTapped:(UIButton *)sender;//反方向解析- (IBAction)reverseTapped:(UIButton *)sender;//地理编码@property (strong, nonatomic) CLGeocoder *geocoder;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // 创建地址解析器 self.geocoder = [[CLGeocoder alloc] init];}#pragma mark - 解析地址- (IBAction)encodeTapped:(UIButton *)sender{ // 获取用户输入的地址字符串 NSString *addr = self.addrField.text; if(addr != nil && addr.length > 0) { [self.geocoder geocodeAddressString:@"北京八维研修学院" completionHandler: ^(NSArray *placemarks, NSError *error) { // 如果解析结果的集合元素的个数大于1,表明解析得到了经度、纬度信息 if (placemarks.count > 0) { // 只处理第一个解析结果,实际项目中可使用列表让用户选择 CLPlacemark *placemark = placemarks[0]; CLLocation* location = placemark.location; self.resultView.text = [NSString stringWithFormat: @"%@的经度为:%g,纬度为:%g" , addr , location.coordinate.longitude , location.coordinate.latitude ]; }else // 没有得到解析结果。 { // 使用UIAlertView提醒用户 [[[UIAlertView alloc] initWithTitle:@"提醒"message:@"您输入的地址无法解析" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil] show]; } }]; }}#pragma mark - 反方向解析- (IBAction)reverseTapped:(UIButton *)sender{ NSString* longitudeStr = self.longitudeField.text; NSString* latitudeStr = self.latitudeField.text; if(longitudeStr != nil && longitudeStr.length > 0 && latitudeStr != nil && latitudeStr.length > 0) { // 将用户输入的经度、纬度封装成CLLocation对象 CLLocation* location = [[CLLocation alloc] initWithLatitude:[latitudeStr floatValue] longitude:[longitudeStr floatValue]]; [self.geocoder reverseGeocodeLocation:location completionHandler: ^(NSArray *placemarks, NSError *error) { // 如果解析结果的集合元素的个数大于1,表明解析得到了经度、纬度信息 if (placemarks.count > 0) { // 只处理第一个解析结果,实际项目可使用列表让用户选择 CLPlacemark* placemark = placemarks[0]; // 获取详细地址信息 NSArray* addrArray = [placemark.addressDictionary objectForKey:@"FormattedAddressLines"]; // 将详细地址拼接成一个字符串 NSMutableString* addr = [[NSMutableString alloc] init]; for(int i = 0 ; i < addrArray.count ; i ++) { [addr appendString:addrArray[i]]; } self.resultView.text = [NSString stringWithFormat: @"经度:%g,纬度:%g的地址为:%@" , location.coordinate.longitude , location.coordinate.latitude , addr]; }else{// 没有得到解析结果。 // 使用UIAlertView提醒用户 [[[UIAlertView alloc] initWithTitle:@"提醒"message:@"您输入的地址无法解析" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil] show]; } }]; }}@end
阅读全文
0 0
- Oc CLGeocoder(地图解析)
- iOS CLGeocoder(地图解析)~demo
- 简易地图(MKMapView,CLLocationManagerDelegate,CLGeocoder)
- iOS5地图使用MapKit,使用CLGeocoder解析,原来的MKReverseGeocoder过时不可用
- CLGeocoder反解析用户的地址
- 定位经纬度解析城市信息 CLGeocoder /CLLocationManager
- oc地图
- [第1章]地图:CoreLocation的使用——CLGeocoder
- OC地图简洁版
- oc地图带字典
- Oc LocateMap(地图定位)
- Oc 地图锚点
- Oc 地图覆盖层
- CLGeocoder基本使用
- 基于CLGeocoder - 地理编码
- OC之XML解析
- oc block解析
- oc 歌词解析
- MVP学习笔记
- 页面滚动条的实现
- mysql修改表,添加列,修改类型,修改列,修改表名,修改表的字符集,sql添加记录,重启mysql命令
- Codeforces Round #443 (Div. 2)D. Teams Formation详解
- Python——二叉树的深度遍历
- Oc CLGeocoder(地图解析)
- Swing 工具常用总结
- Mongoose增查改删学习笔记
- Unity3D游戏场景之鼠标点击地面控制人物移动
- Oc 地图锚点
- 类的创建并实例化
- SQL 添加中文记录;SQL 删除记录;删除所有内容(记录);delete和truncate的区别
- TCP的三次握手
- JAVA面向对象练习