#import <Foundation/Foundation.h> /* .h 文件中导入以下两个框架 */ #import <MapKit/MapKit.h> #import <CoreLocation/CoreLocation.h> @interface CurentLocation : NSObject<MKMapViewDelegate,CLLocationManagerDelegate> @property(nonatomic,strong) CLLocationManager *locaManager; //获取定位信息 -(void)getUSerLocation; + (CurentLocation *)sharedManager; @end /*.m文件中实现代码如下*/ #import "CurentLocation.h" @implementation CurentLocation //定位回调代理 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { for(CLLocation *location in locations){ NSLog(@"---------%@-------",location); } CLLocation *currLocation=[locations lastObject]; CLGeocoder *geoCoder = [[CLGeocoder alloc]init];//反向解析,根据及纬度反向解析出地址 CLLocation *location = [locations objectAtIndex:0]; [geoCoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) { for(CLPlacemark *place in placemarks) { //取出当前位置的坐标 NSLog(@"latitude : %f,longitude: %f",currLocation.coordinate.latitude,currLocation.coordinate.longitude); NSString *latStr = [NSString stringWithFormat:@"%f",currLocation.coordinate.latitude]; NSString *lngStr = [NSString stringWithFormat:@"%f",currLocation.coordinate.longitude]; NSDictionary *dict = [place addressDictionary]; NSMutableDictionary *resultDic = [[NSMutableDictionary alloc] init]; [resultDic setObject:dict[@"SubLocality"] forKey:@"xian"]; [resultDic setObject:dict[@"City"] forKey:@"shi"]; [resultDic setObject:latStr forKey:@"wei"]; [resultDic setObject:lngStr forKey:@"jing"]; [resultDic setObject:dict[@"State"] forKey:@"sheng"]; [resultDic setObject:dict[@"Name"] forKey:@"all"]; NSLog(@"------addressDictionary-%@------",dict); [[NSUserDefaults standardUserDefaults] setObject:dict[@"SubLocality"] forKey:@"XianUser"]; [[NSUserDefaults standardUserDefaults] setObject:resultDic forKey:@"LocationInfo"]; [[NSUserDefaults standardUserDefaults] synchronize]; } }]; } #pragma mark - 检测应用是否开启定位服务 - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { [manager stopUpdatingLocation]; switch([error code]) { case kCLErrorDenied: [self openGPSTips]; break; case kCLErrorLocationUnknown: break; default: break; } } -(void)openGPSTips{ UIAlertView *alet = [[UIAlertView alloc] initWithTitle:@"当前定位服务不可用" message:@"请到“设置->隐私->定位服务”中开启定位" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil]; [alet show]; } //获取定位信息 -(void)getUSerLocation{ //初始化定位管理类 _locaManager = [[CLLocationManager alloc] init]; //delegate _locaManager.delegate = self; //The desired location accuracy.//精确度 _locaManager.desiredAccuracy = kCLLocationAccuracyBest; //Specifies the minimum update distance in meters. //距离 _locaManager.distanceFilter = 10; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) { [_locaManager requestWhenInUseAuthorization]; [_locaManager requestAlwaysAuthorization]; } //开始定位 [_locaManager startUpdatingLocation]; } + (CurentLocation *)sharedManager{ static CurentLocation *sharedAccountManagerInstance = nil; static dispatch_once_t predicate; dispatch_once(&predicate, ^{ sharedAccountManagerInstance = [[self alloc] init]; }); return sharedAccountManagerInstance; } @end
原文:http://my.oschina.net/LiuChongYang/blog/516304