博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[IOS地图开发系类]2、位置解码CLGeocoder
阅读量:5951 次
发布时间:2019-06-19

本文共 6931 字,大约阅读时间需要 23 分钟。

hot3.png

 接的操作,获取到地址信息经纬度后,我们可以对其进行解码,解码采用的CLGeocoder这个类,使用方式如下:

1、在ViewControlelr.m文件中声明一个CLGeocoder的属性,给页面加一个button,然后在处理方法中,对上一篇获取的地址坐标进行解码,

code如下:

////  ViewController.m//  LBS_001_CLLocationManager////  Created by liqun on 13-7-17.//  Copyright (c) 2013年 Block Cheng. All rights reserved.//#import "ViewController.h"#import 
#import
@interface ViewController ()
{}@property (nonatomic,retain)CLLocationManager* locationManager;@property (nonatomic,retain)CLLocation* location;@property (nonatomic, retain) CLGeocoder *myGeocoder;@end@implementation ViewController-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { _locationManager = [[CLLocationManager alloc] init]; } return self;}- (void)dealloc{ self.locationManager = nil; self.location = nil; self.myGeocoder = nil; [super dealloc];}- (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //delegate self.locationManager.delegate = self; //The desired location accuracy. self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; //Specifies the minimum update distance in meters. self.locationManager.distanceFilter = kCLDistanceFilterNone; self.locationManager.purpose = @"To provide functionality based on user's current location."; [self.locationManager startUpdatingLocation]; UIButton *addBt = [UIButton buttonWithType:UIButtonTypeRoundedRect]; addBt.frame = CGRectMake(0, 00, 320, 50); [addBt setTitle:@"locationMe" forState:UIControlStateNormal]; [addBt addTarget:self action:@selector(handleLocationMe:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:addBt]; self.myGeocoder = [[CLGeocoder alloc] init];}- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{ NSLog(@"didChangeAuthorizationStatus---%u",status);}- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{ NSLog(@"didChangeAuthorizationStatus----%@",error);}- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{ UIAlertView* av = [[UIAlertView alloc] initWithTitle:@"update" message:[NSString stringWithFormat:@"didUpdateToLocation: newLocation: %@ old:%@",newLocation,oldLocation] delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil]; [av show]; [av release]; self.location = newLocation; NSLog(@"didUpdateToLocation: newLocation: %@",newLocation);}- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}-(IBAction)handleLocationMe:(id)sender{ [self.myGeocoder reverseGeocodeLocation:self.location completionHandler:^(NSArray *placemarks, NSError *error) { if (error == nil &&[placemarks count] > 0){ CLPlacemark *placemark = [placemarks objectAtIndex:0]; /* We received the results */ NSLog(@"Country = %@", placemark.country); NSLog(@"Postal Code = %@", placemark.postalCode); NSLog(@"Locality = %@", placemark.locality); NSLog(@"dic = %@", placemark.addressDictionary ); NSLog(@"dic FormattedAddressLines= %@", [placemark.addressDictionary objectForKey:@"FormattedAddressLines"]); NSLog(@"dic Name = %@", [placemark.addressDictionary objectForKey:@"Name"]); NSLog(@"dic State = %@", [placemark.addressDictionary objectForKey:@"State"]); NSLog(@"dic Street = %@", [placemark.addressDictionary objectForKey:@"Street"]); NSLog(@"dic SubLocality= %@", [placemark.addressDictionary objectForKey:@"SubLocality"]); NSLog(@"dic SubThoroughfare= %@", [placemark.addressDictionary objectForKey:@"SubThoroughfare"]); NSLog(@"dic Thoroughfare = %@", [placemark.addressDictionary objectForKey:@"Thoroughfare"]); } else if (error == nil && [placemarks count] == 0){ NSLog(@"No results were returned."); } else if (error != nil){ NSLog(@"An error occurred = %@", error); } }];}@end
日志输出如下:

2013-07-28 20:47:16.578 LBS_001_CLLocationManager[30123:907] didChangeAuthorizationStatus---32013-07-28 20:47:17.288 LBS_001_CLLocationManager[30123:907] didUpdateToLocation:  newLocation: <+39.93751238,+116.34143052> +/- 65.00m (speed -1.00 mps / course -1.00) @ 13-7-28 中国标准时间下午8时47分16秒2013-07-28 20:47:17.300 LBS_001_CLLocationManager[30123:907] didUpdateToLocation:  newLocation: <+39.93751238,+116.34143052> +/- 65.00m (speed -1.00 mps / course -1.00) @ 13-7-28 中国标准时间下午8时47分16秒2013-07-28 20:47:17.351 LBS_001_CLLocationManager[30123:907] didUpdateToLocation:  newLocation: <+39.93770077,+116.34199932> +/- 70.88m (speed -1.00 mps / course -1.00) @ 13-7-28 中国标准时间下午8时47分16秒2013-07-28 20:47:17.392 LBS_001_CLLocationManager[30123:907] didUpdateToLocation:  newLocation: <+39.93767287,+116.34189085> +/- 65.00m (speed -1.00 mps / course -1.00) @ 13-7-28 中国标准时间下午8时47分17秒2013-07-28 20:47:51.976 LBS_001_CLLocationManager[30123:907] Country = 中国2013-07-28 20:47:51.977 LBS_001_CLLocationManager[30123:907] Postal Code = (null)2013-07-28 20:47:51.979 LBS_001_CLLocationManager[30123:907] Locality = (null)2013-07-28 20:47:51.980 LBS_001_CLLocationManager[30123:907] dic = {    Country = "\U4e2d\U56fd";    CountryCode = CN;    FormattedAddressLines =     (        "\U4e2d\U56fd\U5317\U4eac\U5e02\U897f\U57ce\U533a\U5c55\U89c8\U8def\U8857\U9053\U897f\U76f4\U95e8\U5916\U5927\U8857132\U53f7"    );    Name = "\U4e2d\U56fd\U5317\U4eac\U5e02\U897f\U57ce\U533a\U5c55\U89c8\U8def\U8857\U9053\U897f\U76f4\U95e8\U5916\U5927\U8857132\U53f7";    State = "\U5317\U4eac\U5e02";    Street = "\U897f\U76f4\U95e8\U5916\U5927\U8857 132\U53f7";    SubLocality = "\U897f\U57ce\U533a";    SubThoroughfare = "132\U53f7";    Thoroughfare = "\U897f\U76f4\U95e8\U5916\U5927\U8857";}2013-07-28 20:47:51.982 LBS_001_CLLocationManager[30123:907] dic FormattedAddressLines= (    "\U4e2d\U56fd\U5317\U4eac\U5e02\U897f\U57ce\U533a\U5c55\U89c8\U8def\U8857\U9053\U897f\U76f4\U95e8\U5916\U5927\U8857132\U53f7")2013-07-28 20:47:51.983 LBS_001_CLLocationManager[30123:907] dic Name = 中国北京市西城区展览路街道西直门外大街132号2013-07-28 20:47:51.985 LBS_001_CLLocationManager[30123:907] dic State = 北京市2013-07-28 20:47:51.986 LBS_001_CLLocationManager[30123:907] dic Street = 西直门外大街 132号2013-07-28 20:47:51.987 LBS_001_CLLocationManager[30123:907] dic SubLocality= 西城区2013-07-28 20:47:51.988 LBS_001_CLLocationManager[30123:907] dic SubThoroughfare= 132号2013-07-28 20:47:51.989 LBS_001_CLLocationManager[30123:907] dic Thoroughfare = 西直门外大街

转载于:https://my.oschina.net/chengliqun/blog/147871

你可能感兴趣的文章
FreeRTOS(一)——任务管理
查看>>
移动端网页怎么做?
查看>>
第5章 高效的多线程日志
查看>>
协议 - 收藏集 - 掘金
查看>>
Kotlin教程 - 收藏集 - 掘金
查看>>
deferred对象
查看>>
2017年3月份前端资源分享
查看>>
Node学习记录: 图片爬虫
查看>>
cookie与session的区别与联系
查看>>
黄东旭:When TiDB Meets Kubernetes
查看>>
有趣的6种图片灰度转换算法
查看>>
Spring Boot 框架介绍和使用
查看>>
使用Angular与TypeScript构建Electron应用(二)
查看>>
Reactjs不能忽略的key
查看>>
关于lazyman你还应该知道这几件事
查看>>
Gandi下配置Github pages的自定义域名
查看>>
深度学习框架不能“包治百病”,开发者如何选出最适合自己的?
查看>>
Hyperledger Composer评测
查看>>
云监控状态调查:公有云和混合云的监控成熟度落后于传统数据中心
查看>>
C# 8的Ranges和递归模式
查看>>