iOS 8 定位
2015-07-27 16:11
567 查看
1.首先导入CoreLocation框架 设置代理
CLLocationManagerDelegate
2.
- (void)initLocationManager
{
BOOL isEnable = [CLLocationManager locationServicesEnabled];
CGFloat verson = [[UIDevice currentDevice].systemVersion doubleValue];//float
if(isEnable) {
if(!_loactionManager){
_loactionManager = [[CLLocationManager alloc] init];
[_loactionManager setDelegate:self];
_loactionManager.desiredAccuracy = kCLLocationAccuracyBest;
_loactionManager.distanceFilter = 20.0;//每隔10米定位一次
if(verson > 8.0f){
[_loactionManager requestAlwaysAuthorization];
}
[_loactionManager startUpdatingLocation];
}
}
else {
Alert(@"温馨提示", @"当前设备定位功能未开启!,建议您到手机系统的[设置]->[隐私]->[定位服务]中打开定位服务,并允许“XXXX社区”使用定位服务" );
return;
}
}
3.要在pist文件中添加两个key值
NSLocationAlwaysUsageDescription NSLocationWhenInUseUsageDescription 这两个key值添加在这个目录下 Application requires iPhone environment
4.设置代理方法
#pragma mark 代理方法
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
CLLocation *currentLocation = [locations lastObject];
MJLog(@"currentLocation == %@",currentLocation);
// 创建位置
CLGeocoder *revGeo = [[CLGeocoder alloc] init];
[revGeo reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
// 地理位置反编
if (!error && [placemarks count] > 0) {
NSDictionary *dict = [[placemarks objectAtIndex:0] addressDictionary];
_currentPosition = [dict objectForKey:@"City"];
MJLog(@"City address: %@",_currentPosition); }
else {
Alert(@"温馨提示", @"获取位置失败");
MJLog(@"ERROR: %@", error); }
}];
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
MJLog(@"error :%@",[error localizedDescription]);
}
CLLocationManagerDelegate
2.
- (void)initLocationManager
{
BOOL isEnable = [CLLocationManager locationServicesEnabled];
CGFloat verson = [[UIDevice currentDevice].systemVersion doubleValue];//float
if(isEnable) {
if(!_loactionManager){
_loactionManager = [[CLLocationManager alloc] init];
[_loactionManager setDelegate:self];
_loactionManager.desiredAccuracy = kCLLocationAccuracyBest;
_loactionManager.distanceFilter = 20.0;//每隔10米定位一次
if(verson > 8.0f){
[_loactionManager requestAlwaysAuthorization];
}
[_loactionManager startUpdatingLocation];
}
}
else {
Alert(@"温馨提示", @"当前设备定位功能未开启!,建议您到手机系统的[设置]->[隐私]->[定位服务]中打开定位服务,并允许“XXXX社区”使用定位服务" );
return;
}
}
3.要在pist文件中添加两个key值
NSLocationAlwaysUsageDescription NSLocationWhenInUseUsageDescription 这两个key值添加在这个目录下 Application requires iPhone environment
4.设置代理方法
#pragma mark 代理方法
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
CLLocation *currentLocation = [locations lastObject];
MJLog(@"currentLocation == %@",currentLocation);
// 创建位置
CLGeocoder *revGeo = [[CLGeocoder alloc] init];
[revGeo reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
// 地理位置反编
if (!error && [placemarks count] > 0) {
NSDictionary *dict = [[placemarks objectAtIndex:0] addressDictionary];
_currentPosition = [dict objectForKey:@"City"];
MJLog(@"City address: %@",_currentPosition); }
else {
Alert(@"温馨提示", @"获取位置失败");
MJLog(@"ERROR: %@", error); }
}];
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
MJLog(@"error :%@",[error localizedDescription]);
}
相关文章推荐
- iOS- 用MapKit和CoreLocation 来实现移动设备(地图与定位)
- iOS开发动画(Animation)图片360度不停旋转
- IOS开发—NSDateFormatter
- 如何用IOS调用WebService(SOAP接口)
- iOS_nil、Nil、NULL、NSNull的区别
- IOS 设备常亮
- 27个提升效率的iOS开源库推荐
- iOS8扩展插件开发配置
- iOS屏幕强制横屏,如右转或左转
- IOS-蓝牙
- iOS Sprite Kit教程之场景的设置
- iOS中Cookie介绍
- 修改storyboard中视图的坐标
- 正则表达式
- IOS第四天(3:数组的排序和乱序)
- iOS开发之layoutSubviews的作用和调用机制
- iOS Sprite Kit教程之场景的设置
- IOS socket开发基础
- IOS第四天(2:字典转模型plist)
- IOS第四天(1:图片的方法和缩小,遮罩层)