首页 > 代码库 > 位置与地图(一)定位获取位置及位置反编码
位置与地图(一)定位获取位置及位置反编码
*我们的应用程序,可以通过添加Core Location框架所包含的类,获取设备的地图位置.
*添加CoreLocation.framework框架,导入#import<CoreLocation/CoreLocation.h>
*使用地图服务时,会消耗更多地设备电量.因此,在获取到设备的位置后,应该停止定位来节省电量
@跟往常一样,我们通过一个demo来展示内容与效果
// // HMTRootViewController.h // My-GPS-Map // // Created by lanou3g on 14-4-12. // Copyright (c) 2014年 胡明涛. All rights reserved. // #import <UIKit/UIKit.h> @interface HMTRootViewController : UIViewController <CLLocationManagerDelegate> @end // // HMTRootViewController.m // My-GPS-Map // // Created by lanou3g on 14-4-12. // Copyright (c) 2014年 胡明涛. All rights reserved. // #import "HMTRootViewController.h" #import <AddressBook/AddressBook.h> @interface HMTRootViewController (){ CLLocationManager * _locationManage; } @property (nonatomic,retain) CLLocationManager * locationManage; @end @implementation HMTRootViewController - (void)dealloc{ RELEASE_SAFELY(_locationManage); [super dealloc]; } - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self createGPSMap]; self.view.backgroundColor = [UIColor redColor]; } - (void)createGPSMap{ // 初始化位置服务 self.locationManage = [[CLLocationManager alloc]init]; // 要求CLLocationManager对象返回全部信息 _locationManage.distanceFilter = kCLDistanceFilterNone; // 设置定位精度 _locationManage.desiredAccuracy = kCLLocationAccuracyBest; // 设置代理 _locationManage.delegate = self; // 开始定位 [_locationManage startUpdatingLocation]; [_locationManage release]; } - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ CLLocation * newLocation = [locations lastObject]; // 停止实时定位 [_locationManage stopUpdatingLocation]; // 取得经纬度 CLLocationCoordinate2D coord2D = newLocation.coordinate; double latitude = coord2D.latitude; double longitude = coord2D.longitude; NSLog(@"纬度 = %f 经度 = %f",latitude,longitude); // 取得精度 CLLocationAccuracy horizontal = newLocation.horizontalAccuracy; CLLocationAccuracy vertical = newLocation.verticalAccuracy; NSLog(@"水平方 = %f 垂直方 = %f",horizontal,vertical); // 取得高度 CLLocationDistance altitude = newLocation.altitude; NSLog(@"%f",altitude); // 取得此时时刻 NSDate *timestamp = [newLocation timestamp]; // 实例化一个NSDateFormatter对象 NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init]; // 设定时间格式 [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss a"]; [dateFormat setAMSymbol:@"AM"]; // 显示中文, 改成"上午" [dateFormat setPMSymbol:@"PM"]; // 求出当天的时间字符串,当更改时间格式时,时间字符串也能随之改变 NSString *dateString = [dateFormat stringFromDate:timestamp]; NSLog(@"此时此刻时间 = %@",dateString); // -----------------------------------------位置反编码-------------------------------------------- CLGeocoder * geocoder = [[CLGeocoder alloc]init]; [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) { for (CLPlacemark * place in placemarks) { NSLog(@"name = %@",place.name); // 位置名 NSLog(@"thoroughfare = %@",place.thoroughfare); // 街道 NSLog(@"subAdministrativeArea = %@",place.subAdministrativeArea); // 子街道 NSLog(@"locality = %@",place.locality); // 市 NSLog(@"subLocality = %@",place.subLocality); // 区 NSLog(@"country = %@",place.country); // 国家 NSArray *allKeys = place.addressDictionary.allKeys; for (NSString *key in allKeys) { NSLog(@"key = %@, value = http://www.mamicode.com/%@",key, place.addressDictionary[key]);>@程序运行结果:(以39.3,116.4为例)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。