美文网首页
iOS 开发 CoreLocation实现定位功能

iOS 开发 CoreLocation实现定位功能

作者: 汤玉阳Scofield | 来源:发表于2016-12-16 11:26 被阅读0次

1、首先得引入CoreLocation系统框架

屏幕快照 2016-12-16 上午11.17.04.png

2、导入头文件 遵循代理

import <CoreLocation/CoreLocation.h>

<CLLocationManagerDelegate>

3、在info.plist中打开定位权限

屏幕快照 2016-12-16 上午11.22.43.png

3、代码实现

@property (nonatomic, strong) CLLocationManager* locationManager;

- (void)viewDidLoad {
    [super viewDidLoad];
//检测定位功能是否开启
    if([CLLocationManager locationServicesEnabled]){
        
        if(!_locationManager){
            
            self.locationManager = [[CLLocationManager alloc] init];
            
            if([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]){
                [self.locationManager requestWhenInUseAuthorization];
                [self.locationManager requestAlwaysAuthorization];
                
            }
            
            //设置代理
            [self.locationManager setDelegate:self];
            //设置定位精度
            [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
            //设置距离筛选
            [self.locationManager setDistanceFilter:100];
            //开始定位
            [self.locationManager startUpdatingLocation];
            //设置开始识别方向
            [self.locationManager startUpdatingHeading];
            
        }
        
    }else{
        UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"您没有开启定位功能" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        [alertView addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
        [self presentViewController:alertView animated:YES completion:nil];

    }
}

这里返回一个[placemark addressDictionary]的字典 打印一下可以看到字典里面的各种key,想用哪个key直接替换掉即可

//反地理编码
- (void)reverseGeocoder:(CLLocation *)currentLocation {
    
    CLGeocoder* geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        
        NSLog(@"array == %@",placemarks);
        
        if(error || placemarks.count == 0){
            NSLog(@"error = %@",error);
        }else{
            CLPlacemark* placemark = placemarks.firstObject;
            NSLog(@"dic == %@",[placemark addressDictionary]);
            NSLog(@"placemark:%@",[[placemark addressDictionary] objectForKey:@"Street"]);
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"您的位置" message:[[placemark addressDictionary] objectForKey:@"Street"] preferredStyle:UIAlertControllerStyleAlert];
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
            [self presentViewController:alert animated:YES completion:nil];
        }  
    }];  
}
//定位成功以后调用
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    [self.locationManager stopUpdatingLocation];
    CLLocation* location = locations.lastObject;
    [self reverseGeocoder:location];
}

相关文章

  • 定位

    定位 1.实现定位功能 在iOS中使用定位功能,需要导入CoreLocation.h文件,其实现定位功能的步骤如下...

  • iOS 开发 CoreLocation实现定位功能

    1、首先得引入CoreLocation系统框架 2、导入头文件 遵循代理 import

  • iOS中的定位功能

    iOS中的定位功能 CoreLocation框架(CoreLocation.framework)可用于定位设备当前...

  • 百度地图之定位

    iOS中三种定位方式 ioS开发之CoreLocation(GPS定位) iOS自带的GPS 定位 iOS开发 C...

  • CoreLocation定位

    定位 -在iOS开发中想要加入定位和地图功能,那么必须基于CoreLocation和MapKit2个框架进行开发-...

  • iOS CoreLocation 定位实现

    CoreLocation 定位 iOS 地图 反编码 CoreLocation 实现基于 8.0 之后的使用方法,...

  • 地图(一)之CoreLocation

    CoreLocation CoreLocation用于地理定位,地理编码区域监听等(着重功能实现) 1.获取定位授...

  • IOS(swift)获取用户定位及坐标转换

    前言 IOS开发中经常会需要使用定位的功能,主要使用的是CoreLocation.framework。 具体使用 ...

  • 经验拾遗之定位功能

    iOS开发,定位功能是很多app的核心功能,也是出门在外的必用功能,现在来重新整理一下CoreLocation这...

  • iOS Core Location 开篇

    一、简介 在iOS开发中,要想加入定位和地图这2大功能,必须基于2个框架进行开发CoreLocation:用于地理...

网友评论

      本文标题:iOS 开发 CoreLocation实现定位功能

      本文链接:https://www.haomeiwen.com/subject/otjnmttx.html