首先在plist文件中
但是对于8.0之后的9.0之间的版本有时在设置中更改定位权限会闪退 改成下面的就好啦
然后在.h文件中:
//
// CCLocationManager.h
// MMLocationManager
//
//
// Created by GuoFeng on 16/4/14.
// Copyright © 2016年 WXDL. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#define CCLastLongitude @"CCLastLongitude"
#define CCLastLatitude @"CCLastLatitude"
#define CCLastCity @"CCLastCity"
#define CCLastAddress @"CCLastAddress"
typedef void (^LocationBlock)(CLLocationCoordinate2D locationCorrrdinate);
typedef void (^LocationErrorBlock) (NSError *error);
typedef void(^NSStringBlock)(NSString *cityString);
typedef void(^NSStringBlock)(NSString *addressString);
@interface CCLocationManager :NSObject<CLLocationManagerDelegate>
@property (nonatomic)CLLocationCoordinate2D lastCoordinate;
@property(nonatomic,strong)NSString *lastCity;
@property (nonatomic,strong)NSString *lastAddress;
@property(nonatomic,assign)float latitude;
@property(nonatomic,assign)float longitude;
+ (CCLocationManager *)shareLocation;
/**
* 获取坐标
*
* @param locaiontBlock locaiontBlock description
*/
- (void) getLocationCoordinate:(LocationBlock) locaiontBlock ;
/**
* 获取坐标和详细地址
*
* @param locaiontBlock locaiontBlock description
* @param addressBlock addressBlock description
*/
- (void) getLocationCoordinate:(LocationBlock) locaiontBlock withAddress:(NSStringBlock) addressBlock;
/**
* 获取详细地址
*
* @param addressBlock addressBlock description
*/
- (void) getAddress:(NSStringBlock)addressBlock;
/**
* 获取城市
*
* @param cityBlock cityBlock description
*/
- (void) getCity:(NSStringBlock)cityBlock;
///**
// * 获取城市和定位失败
// *
// * @param cityBlock cityBlock description
// * @param errorBlock errorBlock description
// */
//- (void) getCity:(NSStringBlock)cityBlock error:(LocationErrorBlock) errorBlock;
@end
//
// CCLocationManager.m
// MMLocationManager
//
//
// Created by GuoFeng on 16/4/14.
// Copyright © 2016年 WXDL. All rights reserved.
//
#import "CCLocationManager.h"
@interface CCLocationManager (){
CLLocationManager *_manager;
}
@property (nonatomic,strong) LocationBlock locationBlock;
@property (nonatomic,strong) NSStringBlock cityBlock;
@property (nonatomic,strong) NSStringBlock addressBlock;
@property (nonatomic,strong) LocationErrorBlock errorBlock;
@end
@implementation CCLocationManager
+ (CCLocationManager *)shareLocation{
staticdispatch_once_t pred = 0;
__strongstatic id _sharedObject =nil;
dispatch_once(&pred, ^{
_sharedObject = [[self alloc] init];
});
return _sharedObject;
}
- (id)init {
self = [super init];
if (self) {
NSUserDefaults *standard = [NSUserDefaults standardUserDefaults];
float longitude = [standard floatForKey:CCLastLongitude];
float latitude = [standard floatForKey:CCLastLatitude];
self.longitude = longitude;
self.latitude = latitude;
self.lastCoordinate = CLLocationCoordinate2DMake(longitude,latitude);
self.lastCity = [standard objectForKey:CCLastCity];
self.lastAddress=[standard objectForKey:CCLastAddress];
}
returnself;
}
//获取经纬度
- (void) getLocationCoordinate:(LocationBlock) locaiontBlock
{
self.locationBlock = [locaiontBlock copy];
[self startLocation];
}
- (void) getLocationCoordinate:(LocationBlock) locaiontBlock withAddress:(NSStringBlock) addressBlock
{
self.locationBlock = [locaiontBlock copy];
self.addressBlock = [addressBlock copy];
[self startLocation];
}
- (void) getAddress:(NSStringBlock)addressBlock
{
self.addressBlock = [addressBlock copy];
[self startLocation];
}
//获取省市
- (void) getCity:(NSStringBlock)cityBlock
{
self.cityBlock = [cityBlock copy];
[self startLocation];
}
//- (void) getCity:(NSStringBlock)cityBlock error:(LocationErrorBlock) errorBlock
//{
// self.cityBlock = [cityBlock copy];
// self.errorBlock = [errorBlock copy];
// [self startLocation];
//}
#pragma mark CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSUserDefaults *standard = [NSUserDefaults standardUserDefaults];
CLGeocoder *geocoder=[[CLGeocoder alloc]init];
[geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks,NSError *error)
{
if (placemarks.count >0) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
_lastCity = [NSString stringWithFormat:@"%@%@",placemark.administrativeArea,placemark.locality];
[standard setObject:_lastCity forKey:CCLastCity];//省市地址
NSLog(@"______%@",_lastCity);
_lastAddress = [NSString stringWithFormat:@"%@%@%@%@%@%@",placemark.country,placemark.administrativeArea,placemark.locality,placemark.subLocality,placemark.thoroughfare,placemark.subThoroughfare];//详细地址
if (placemark.subThoroughfare.length==0) {
_lastAddress = [NSString stringWithFormat:@"%@%@%@%@%@",placemark.country,placemark.administrativeArea,placemark.locality,placemark.subLocality,placemark.thoroughfare];
}
NSLog(@"______%@",_lastAddress);
}
if (_cityBlock) {
_cityBlock(_lastCity);
_cityBlock = nil;
}
if (_addressBlock) {
_addressBlock(_lastAddress);
_addressBlock = nil;
}
}];
_lastCoordinate = CLLocationCoordinate2DMake(newLocation.coordinate.latitude ,newLocation.coordinate.longitude );
if (_locationBlock) {
_locationBlock(_lastCoordinate);
_locationBlock = nil;
}
NSLog(@"%f--%f",newLocation.coordinate.latitude,newLocation.coordinate.longitude);
[standard setObject:@(newLocation.coordinate.latitude) forKey:CCLastLatitude];
[standard setObject:@(newLocation.coordinate.longitude) forKey:CCLastLongitude];
[manager stopUpdatingLocation];
}
-(void)startLocation
{
if([CLLocationManager locationServicesEnabled] && [CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied)
{
_manager=[[CLLocationManager alloc]init];
_manager.delegate=self;
_manager.desiredAccuracy = kCLLocationAccuracyBest;
[_manager requestAlwaysAuthorization];
_manager.distanceFilter=100;
[_manager startUpdatingLocation];
}
else
{
UIAlertView *alvertView=[[UIAlertView alloc]initWithTitle:@"提示" message:@"需要开启定位服务,请到设置->隐私,打开定位服务" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
[alvertView show];
}
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error{
[self stopLocation];
}
-(void)stopLocation
{
_manager = nil;
}
@end
#import "CCLocationManager.h"
在需要用的地方:
-(void)addressInfo
{
if (IS_IOS8) {
[UIApplicationsharedApplication].idleTimerDisabled =TRUE;
locationmanager = [[CLLocationManageralloc] init];
[locationmanagerrequestAlwaysAuthorization]; //NSLocationAlwaysUsageDescription
[locationmanagerrequestWhenInUseAuthorization]; //NSLocationWhenInUseDescription
locationmanager.delegate =self;
}
//__block __weak MainTabbarController *wself = self;
__blockNSString *string;
if (IS_IOS8) {
[[CCLocationManagershareLocation]getLocationCoordinate:^(CLLocationCoordinate2D locationCorrrdinate) {
string = [NSStringstringWithFormat:@"%f %f",locationCorrrdinate.latitude,locationCorrrdinate.longitude];
_lat=[NSStringstringWithFormat:@"%f",locationCorrrdinate.latitude];
_long=[NSStringstringWithFormat:@"%f",locationCorrrdinate.longitude];
} withAddress:^(NSString *addressString) {
NSLog(@"+++++++++%@",addressString);
string = [NSStringstringWithFormat:@"%@\n%@",string,addressString];
// [wself setLabelText:string];
_addressinfo=addressString;
}
}];
}
}