// // LocationManager.m // CParam // // Created by breeze deng on 2017/8/7. // Copyright © 2017年 Breeze. All rights reserved. // #import "LocationManager.h" static LocationManager *_instance = nil; @interface LocationManager () @property (nonatomic, strong) CLLocationManager* locationManager; @end @implementation LocationManager + (LocationManager *)shareInstance { @synchronized(self) { // 实例对象只分配一次 if(_instance == nil) { _instance = [[super allocWithZone:NULL] init]; } } return _instance; } + (id)allocWithZone:(NSZone *)zone { return [self shareInstance]; } - (id)copyWithZone:(NSZone *)zone { return self; } - (CLLocationManager *)locationManager { if (_locationManager == nil) { _locationManager = [[CLLocationManager alloc] init]; _locationManager.delegate = self; _locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; } return _locationManager; } - (void)startUpdatingLocation { if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { NSLog(@"requestWhenInUseAuthorization =======:"); [self.locationManager requestWhenInUseAuthorization]; } //开始定位,不断调用其代理方法 [self.locationManager startUpdatingLocation]; } - (void)stopUpdatingLocation { [self.locationManager stopUpdatingLocation]; } - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { // 1.获取用户位置的对象 CLLocation *location = [locations lastObject]; CLLocationCoordinate2D coordinate = location.coordinate; if ([self.delegate respondsToSelector: @selector(updateLocationWithCoordinate:)]) { [self.delegate updateLocationWithCoordinate: coordinate]; } // 2.停止定位 [manager stopUpdatingLocation]; } @end