标签:
1 定位服务编码
使用Core Location框架
CLLocationManger:用于定位服务管理类,能够给我们提供位置信息和高度信息,也可以监控设备进入或离开某个区域,还可以获得设备的运行方向等。
CLLocation:封装了位置和高度信息。
CLLocationMangerDelegate
准备:加载Core Location框架
修改工程配置:Supporting Files-Info.pist添加NSLocationAlwaysUSageDescription和NSLocationWhenInUseUsageDescription,自定义文字内容。
初始化locationManager并设置属性-设置用户授权并修改工程配置-开启与关闭-实现委托
- (void)viewDidLoad {
[super viewDidLoad];
//定位服务管理对象初始化
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
设置精确度
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
设置距离过滤器:定义了设备移动后获得位置信息的最小距离,单位是米
self.locationManager.distanceFilter = 1000.0f;
iOS8之后,设置用户授权。为了能够弹出授权对话框,我们需要修改工程配置。
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager requestAlwaysAuthorization];
}
开始定位与停止定位:
iOS6之后通过allowDeferredLocationUpdatesUntilTraveled:timeout:实现延迟更新,disallowDeferredLocationUpdates关闭延迟更新。
pausesLocationUpdatesAutomatically属性设置自动暂停位置更新,把定位服务的开启和关闭交给系统。
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//开始定位,根据设定的条件会不断请求回调新的位置信息,开启要慎重。
[self.locationManager startUpdatingLocation];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
//停止定位
[self.locationManager stopUpdatingLocation];
}
实现委托方法:当用户设备移动的距离超过一定的距离后,就会回调委托方法。
locationManager:didUpdateLocations:定位成功
locationManager:didFailWithError:定位失败
locationManager:didChangeAuthorizationStatus:授权状态发生变化时调用
#pragma mark Core Location委托方法用于实现位置的更新
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 是位置变化的集合,按照时间变化顺序存放
{ 获取设备当前位置
CLLocation *currLocation = [locations lastObject];
获取位置信息CLLocation封装了altitude和coordinate(latitude longitude)
self.txtLat.text = [NSString stringWithFormat:@"%3.5f",
currLocation.coordinate.latitude];纬度
self.txtLng.text = [NSString stringWithFormat:@"%3.5f",
currLocation.coordinate.longitude];经度
self.txtAlt.text = [NSString stringWithFormat:@"%3.5f",
currLocation.altitude];高度
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"error: %@",error);
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{参数是授权信息,也可使用CLLocationManager的静态方法authorizationStatus获得授权信息
if (status == kCLAuthorizationStatusAuthorizedAlways) {
NSLog(@"Authorized");
} else if (status == kCLAuthorizationStatusAuthorizedWhenInUse) {
NSLog(@"AuthorizedWhenInUse");
} else if (status == kCLAuthorizationStatusDenied) {
NSLog(@"Denied");
} else if (status == kCLAuthorizationStatusRestricted) {
NSLog(@"Restricted");
} else if (status == kCLAuthorizationStatusNotDetermined) {
NSLog(@"NotDetermined");
}
}
2 地理信息反编码
通过地理坐标返回某个地点的相关文字描述信息。通过CLGeocoder实现,这个类能够实现地理坐标与文字描述信息之间的转换。
方法:-reverseGeocoderLocation:completionHandler:
CLPlacemark:“地标”类,封装文字描述。
一个地理坐标点是指一个范围。
准备:添加AddressBook.framework,在viewController中添加#import <AddressBook/AddressBook.h>
- (IBAction)reverseGeocode:(id)sender {
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
开始编码
[geocoder reverseGeocodeLocation:self.currLocation
completionHandler:^(NSArray *placemarks, NSError *error) {解析完成后回调,第一个参数是反编码成功的地标集合,第二个参数描述错误信息。
if ([placemarks count] > 0) {判断反编码是否成功
CLPlacemark *placemark = placemarks[0]拿出第一个,或者遍历所有;
地址信息的字典
NSDictionary *addressDictionary = placemark.addressDictionary;
NSString *address = [addressDictionary
objectForKey:(NSString *)kABPersonAddressStreetKey];
address = address == nil ? @"": address;
NSString *state = [addressDictionary
objectForKey:(NSString *)kABPersonAddressStateKey];
state = state == nil ? @"": state;
NSString *city = [addressDictionary
objectForKey:(NSString *)kABPersonAddressCityKey];
city = city == nil ? @"": city;
_txtView.text = [NSString stringWithFormat:@"%@ \n%@ \n%@",state, address,city];
}
}];
}
3 地理信息编码查询
通过地理信息描述查询相关地理坐标,查询结果是个集合。
CLGeocoder类
方法:geocoderAddressDictionary:completionHander:通过地址信息字典对象参数查询
geocoderAddressString:completionHander:
geocoderAddressString:inRegion:completionHandler:通过字符串和范围查询,第二个参数是CLRegion类。
- (IBAction)geocodeQuery:(id)sender {
if (self.txtQueryKey.text == nil || [self.txtQueryKey.text length] == 0) {
return;
}
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:self.txtQueryKey.text completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"查询记录数:%u",(unsigned int)[placemarks count]);
if ([placemarks count] > 0) {
CLPlacemark* placemark = placemarks[0];
NSDictionary *addressDictionary = placemark.addressDictionary;
NSString *address = [addressDictionary
objectForKey:(NSString *)kABPersonAddressStreetKey];
address = address == nil ? @"": address;
NSString *state = [addressDictionary
objectForKey:(NSString *)kABPersonAddressStateKey];
state = state == nil ? @"": state;
NSString *city = [addressDictionary
objectForKey:(NSString *)kABPersonAddressCityKey];
city = city == nil ? @"": city;
CLLocationCoordinate2D coordinate = placemark.location.coordinate;
NSString* strCoordinate = [NSString stringWithFormat:@"经度:%3.5f \n纬度:%3.5f",coordinate.latitude, coordinate.longitude];
self.txtView.text = [NSString stringWithFormat:@"%@ \n %@ \n%@ \n%@",strCoordinate,state, address,city];
//关闭键盘
[self.txtQueryKey resignFirstResponder];
}
}];
}
4 关于定位服务的测试
如果希望模拟器开始运行时就能获得模拟数据,可以在启动参数中设置:Edit Scheme-Run MyLocation.app-options-设置Core Location和Default Location或自己添加GPX文件
连续位置变化测试:模拟器-Debug-Location后面3个菜单项都能发出连续位置变化数据
标签:
原文地址:http://www.cnblogs.com/haugezi/p/4824116.html