建一个NSObject类
在LocationManager.h中
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
@interface LocationManager : NSObject<CLLocationManagerDelegate>
@property (strong, nonatomic) CLLocationManager * locationManager; //申明一个管理器
@property (strong, nonatomic) NSString * city;
@property (strong, nonatomic) void(^locationCityBlock)(NSString *); //定位的城市
@property (strong, nonatomic) void(^locationStreetBlock)(NSString *); //定位的街道
@property (nonatomic,strong) void(^location)(CLLocation *);
+(LocationManager *)sharedManager;
-(void)startLocation;
@end
在LocationManager.m中
#import "LocationManager.h"
@interface LocationManager()<CLLocationManagerDelegate>
@end
@implementation LocationManager
//单利,初始化管理器
+(LocationManager *)sharedManager{
static LocationManager *manager = nil;
static dispatch_once_t token;
dispatch_once(&token,^{
if(manager == nil){
manager = [[LocationManager alloc]init];
manager.locationManager.delegate = manager;
[manager locationSetLayout];
}
} );
return manager;
}
-(void)locationSetLayout{
_locationManager = [[CLLocationManager alloc]init];
_locationManager.delegate=self;
//开始定位
[_locationManager startUpdatingLocation];
_locationManager.desiredAccuracy=kCLLocationAccuracyBest;
CLAuthorizationStatus status =[CLLocationManager authorizationStatus];
//如果未授权则请求
if(status==kCLAuthorizationStatusNotDetermined) {
[_locationManager requestAlwaysAuthorization];
}
}
//开始定位
-(void)startLocation{
[_locationManager startUpdatingLocation];
}
//代理设置
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
CLLocation *location=[locations firstObject];//取出第一个位置
if (self.location) {
self.location(location);
}
CLLocationCoordinate2D coordinate=location.coordinate;//位置坐标
NSLog(@"经度:%f", coordinate.longitude);
NSLog(@"纬度:%f", coordinate.latitude);
NSLog(@"速度:%f 米/秒", location.speed);
//如果不需要实时定位,使用完即使关闭定位服务
[_locationManager stopUpdatingLocation];
//逆地理编码
CLGeocoder *revGeo = [[CLGeocoder alloc] init];
[revGeo reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if (!error && [placemarks count] > 0)
{
NSDictionary *dict =
[[placemarks objectAtIndex:0] addressDictionary];
NSString * cityString = dict[@"City"];
_city = [cityString substringWithRange:NSMakeRange(0, cityString.length -1)];
if (self.locationCityBlock) {
self.locationCityBlock(_city);
}
if (self.locationStreetBlock) {
self.locationStreetBlock(dict[@"Street"]);
}
NSLog(@"dict:%@",dict);
}
else
{
NSLog(@"ERROR: %@", error);
}
}];
}
@end
在ViewControll.m中
#import "ViewController.h"
#import "LocationManager.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
LocationManager * mangaer = [LocationManager sharedManager];
[mangaer setLocation:^(CLLocation * location){
NSLog(@"location:%@",location);
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
还要设置,点击xcode->Debug->Location,点第二个