首先导入CoreLocation.framework
ViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <corelocation/CLLocationManagerDelegate.h>
@interface ViewController : UIViewController<CLLocationManagerDelegate>
//经度
@property (weak, nonatomic) IBOutlet UITextField *txtLng;
//纬度
@property (weak, nonatomic) IBOutlet UITextField *txtLat;
//高度
@property (weak, nonatomic) IBOutlet UITextField *txtAlt;
@property (nonatomic, strong) CLLocationManager *locationManeger;
@end
ViewController.m
#import "ViewController.h"
#import <AddressBook/AddressBook.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UILabel *latlab = [[UILabel alloc]initWithFrame:CGRectMake(20, 60, 80, 40)];
latlab.text = @"纬度";
[self.view addSubview:latlab];
UILabel *lnglab = [[UILabel alloc]initWithFrame:CGRectMake(20, 100, 80, 40)];
lnglab.text = @"经度";
[self.view addSubview:lnglab];
UILabel *altlab = [[UILabel alloc]initWithFrame:CGRectMake(20, 140, 80, 40)];
altlab.text = @"高度";
[self.view addSubview:altlab];
UITextField *latTextFild = [[UITextField alloc]initWithFrame:CGRectMake(90, 60, 210, 38)];
latTextFild.tag = 1;
latTextFild.backgroundColor = [UIColor clearColor];
latTextFild.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:latTextFild];
UITextField *lngTextFild = [[UITextField alloc]initWithFrame:CGRectMake(90, 100, 210, 38)];
lngTextFild.tag = 2;
lngTextFild.backgroundColor = [UIColor clearColor];
lngTextFild.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:lngTextFild];
UITextField *altTextFild = [[UITextField alloc]initWithFrame:CGRectMake(90, 140, 210, 38)];
altTextFild.tag = 3;
altTextFild.backgroundColor = [UIColor clearColor];
altTextFild.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:altTextFild];
UIButton *selectbtn = [[UIButton alloc]initWithFrame:CGRectMake(95, 200, 200, 40)];
selectbtn.tag = 4;
selectbtn.backgroundColor = [UIColor blueColor];
NSString *title = [NSString stringWithFormat:@"地理信息反编码"];
[selectbtn setTitle:title forState: UIControlStateNormal];
[self.view addSubview:selectbtn];
[selectbtn addTarget:self action:@selector(selectaddress:) forControlEvents:UIControlEventTouchUpInside];
UITextView *address = [[UITextView alloc]initWithFrame:CGRectMake(95, 240, 200, 40)];
address.tag = 5;
address.backgroundColor = [UIColor clearColor];
[self.view addSubview:address];
//初始化定位服务管理对象
_locationManeger = [[CLLocationManager alloc]init];
_locationManeger.delegate = self;
//设置desiredAccuracy属性
_locationManeger.desiredAccuracy = kCLLocationAccuracyBest;
//设置distanceFilter属性,他是距离过滤器,定义了设备移动后获得位置信息的最小距离,单位米
_locationManeger.distanceFilter = 1000.0f;
// Do any additional setup after loading the view, typically from a nib.
}
//- (void)reverseGeocode
//{
// // 地理信息反编码
// CLGeocoder *geocoder = [[CLGeocoder alloc] init];
// [geocoder reverseGeocodeLocation:self.currLocation completionHandler:^(NSArray *placemarks, NSError *error) {
// if ([placemarks count] > 0) {
// CLPlacemark *placemark = [placemarks firstObject];
// NSDictionary *addressDic = placemark.addressDictionary;
// NSString *address = [addressDic objectForKey:(NSString *)kABPersonAddressStreetKey];
// address = (address == nil ? @"" : address); // 地址
//
// NSString *state = [addressDic objectForKey:(NSString *)kABPersonAddressStateKey];
// state = (state == nil ? @"" : state); // 省
//
// NSString *city = [addressDic objectForKey:(NSString *)kABPersonAddressCityKey];
// city = (city == nil ? @"" : city); //
// NSLog(@"=%@\n==%@\n===%@\n", state, address, city);
// }
// }];
//}
//
- (IBAction)selectaddress:(id)sender
{
// CLGeocoder *geocoder = [[CLGeocoder alloc]init];
// [geocoder reverseGeocodeLocation:_currentAction completionHandler:^(NSArray *placemarks, NSError *error) {
// if ([placemarks count] > 0) {
// CLPlacemark *placemark = placemarks[0];
// NSDictionary *addressDictionary = placemarks.addressDictionary;
// NSString *address = [addressDictionary objectForKey:(NSString *)KABpersonAddressStreeKey];
//
// }
// }]
}
//初始化CLLocationManager类后,需要使用startUpdatingLocation方法开始定位服务,该方法定义在ViewController.m的viewWillAppear:方法中
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//开始定位
[_locationManeger startUpdatingLocation];
}
//调用startUpdatingLocation方法时,就会开启定位服务。根据条件设定,他不断请求回调新的位置信息。在视图控制器的生命周期方法viewWillAppear:中使用这个方法是最合适的。与开启服务对应的方法是stopUpdatingLocation方法,他是在视图控制器的viewwilldisappear:方法中调用的。
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
//停止定位
[_locationManeger stopUpdatingLocation];
}
//实现CLLocationManager委托的代码如下
#pragma mark Core Location委托方法用于实现位置的更新
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations//位置变化集合 locations 按时间变化顺序存放
{
CLLocation * currLocation = [locations lastObject];//当前设备位置 lastObject 集合返回对象是CLLocation
_txtLat.text = [NSString stringWithFormat:@"%3.5f",currLocation.coordinate.latitude];//coordinate.latitude是CLLocation的一个属性 表示纬度
_txtLng.text = [NSString stringWithFormat:@"%3.5f",currLocation.coordinate.longitude];//coordinate.longitude是CLLocation的一个属性经度
_txtAlt.text = [NSString stringWithFormat:@"%3.5f",currLocation.altitude];//altitude是CLLocation的一个属性 表示高度
/*
coordinate封装的精度纬度结构体CLLocationCoordinate2D 定义如下
typedef struct {
CLLocationDegrees latitude;//纬度
CLLocationDegrees longitude;//经度
}CLLocationCoordinate2D;
CLLocationDegrees是使用type定义的double类型
*/
}
- (void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"error: %@",error);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end