50字范文,内容丰富有趣,生活中的好帮手!
50字范文 > 位置与地图(一)定位获取位置及位置编码-反编码

位置与地图(一)定位获取位置及位置编码-反编码

时间:2021-01-26 02:34:50

相关推荐

位置与地图(一)定位获取位置及位置编码-反编码

*我们的应用程序,可以通过添加Core Location框架所包含的类,获取设备的地图位置.

*添加CoreLocation.framework框架,导入#import<CoreLocation/CoreLocation.h>

*使用地图服务时,会消耗更多地设备电量.因此,在获取到设备的位置后,应该停止定位来节省电量

@跟往常一样,我们通过一个demo来展示内容与效果

//// HMTRootViewController.h// My-GPS-Map//// Created by hmt on 14-4-12.// Copyright (c) 胡明涛. All rights reserved.//#import <UIKit/UIKit.h>@interface HMTRootViewController : UIViewController <CLLocationManagerDelegate>@end//// HMTRootViewController.m// My-GPS-Map//// Created by hmt on 14-4-12.// Copyright (c) 胡明涛. 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 = %@",key, place.addressDictionary[key]);}#pragma mark - 使用系统定义的字符串直接查询,记得导入AddressBook框架NSLog(@"kABPersonAddressCityKey = %@", (NSString *)kABPersonAddressCityKey);NSLog(@"city = %@", place.addressDictionary[(NSString *)kABPersonAddressCityKey]);NSString *city = place.locality;if(city == nil){city = place.addressDictionary[(NSString *)kABPersonAddressStateKey];}}}];}- (void)didReceiveMemoryWarning{[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.}@end

@程序运行结果:(以39.3,116.4为例)

// 判断输入的地址if (self.locationTextField.text == nil || [self.locationTextField.text length] == 0) {return;}CLGeocoder *geocoder = [[CLGeocoder alloc] init];/* -----------------------------------------位置编码-------------------------------------------- */[geocoder geocodeAddressString:_locationTextField.text completionHandler:^(NSArray *placemarks, NSError *error) {for (CLPlacemark *placemark in placemarks) {CLLocationCoordinate2D coordinate = placemark.location.coordinate;NSString *strCoordinate = [NSString stringWithFormat:@"纬度 = %3.5f\n 经度 = %3.5f",coordinate.latitude,coordinate.longitude];NSLog(@"%@",strCoordinate);NSDictionary *addressDictionary = placemark.addressDictionary;NSString *address = [addressDictionary objectForKey:(NSString *)kABPersonAddressStreetKey];NSString *state = [addressDictionary objectForKey:(NSString *)kABPersonAddressStateKey];NSString *city = [addressDictionary objectForKey:(NSString *)kABPersonAddressCityKey];NSLog(@"街道 = %@\n 省 = %@\n 城市 = %@",address,state,city);}}];

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。