首先新建一個空工程 2.在建一個繼承與UIViewController的RootViewController
ios
代碼以下:
git
//設置根視圖管理器 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; RootViewController *rvc = [[RootViewController alloc] init]; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:rvc]; self.window.rootViewController = nav; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; }
在RootViewControllers的.m文件代碼以下
#import "RootViewController.h" #import <CoreLocation/CoreLocation.h> #define PATH @"http://api.map.baidu.com/geocoder?output=json&location=%f,%f&key=dc40f705157725fc98f1fee6a15b6e60" @interface RootViewController ()<CLLocationManagerDelegate> { //lbs 定位管理 CLLocationManager *_manager; } @property (nonatomic,strong)CLLocationManager *manager; @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"開始定位" style:UIBarButtonItemStylePlain target:self action:@selector(begineLocation)]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"中止定位" style:UIBarButtonItemStylePlain target:self action:@selector(endLocation)]; //建立管理器 [self initLocationManager]; } /* kCLLocationAccuracyBestForNavigation //導航時候用的精度 kCLLocationAccuracyBest;//比較高的精度 kCLLocationAccuracyNearestTenMeters; 10m內 kCLLocationAccuracyHundredMeters;100m kCLLocationAccuracyKilometer;1000m kCLLocationAccuracyThreeKilometers; 3000m */ - (void)initLocationManager { //懶加載 if (!self.manager) { //實例化一個管理對象 self.manager = [[CLLocationManager alloc] init]; //設置精度 精度越高越耗電 self.manager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; //精度大小 1m self.manager.distanceFilter = 1; /* iOS8以後 須要設置配置文件 1.在info.plist中添加 Privacy - Location Usage Description , NSLocationAlwaysUsageDescription 2.在代碼中 [_manager requestAlwaysAuthorization]; //ios8特有,申請用戶受權使用地理位置信息 */ CGFloat version = [[[UIDevice currentDevice] systemVersion] floatValue]; //判斷版本 if (version >= 8.0) { //申請用戶受權使用地理位置信息 //Authorization受權的意思 [self.manager requestAlwaysAuthorization]; } //若是要 獲取定位的位置 那麼須要設置代理 self.manager.delegate = self; } } - (void)begineLocation { //是否支持定位服務 if ([CLLocationManager locationServicesEnabled]) { //開始定位 [self.manager startUpdatingLocation]; }else{ NSLog(@"沒有gps"); } } //中止定位 - (void)endLocation { [self.manager stopUpdatingLocation]; } #pragma mark - CLLocationManagerDelegate協議 //當定位開始時 位置發生改變的時候 會一直調用 //會把定位的位置 放入 locations數組中 //這個數組實際上只有一個元素 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { NSLog(@"定位位置"); if (locations.count) { //數組中只有一個元素 CLLocation *location = [locations lastObject]; //CLLocationCoordinate2D是一個結構體 內部存放的是經緯度 CLLocationCoordinate2D coordinate = location.coordinate; NSLog(@"longitude:%f",coordinate.longitude); NSLog(@"latitude:%f",coordinate.latitude); #if 1 //官方自帶的地理反編碼 CLGeocoder *geoCoder = [[CLGeocoder alloc] init]; //地理逆向編碼 地理反編碼 (把經緯度轉化爲真正的地址位置) [geoCoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) { //回調這個block //解析好以後會放在 placemarks 數組中(實際上就一個元素) for (CLPlacemark *mark in placemarks) { NSLog(@"name->%@",mark.name); NSLog(@"country:%@",mark.country); for (NSString *key in mark.addressDictionary) { NSLog(@"%@",mark.addressDictionary[key]); } } }]; #else //異步下載數據 用百度的地理反編碼 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSString *url = [NSString stringWithFormat:PATH,coordinate.latitude, coordinate.longitude]; NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]]; //下載完成 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; NSDictionary *result = dict[@"result"]; NSLog(@"addr:%@",result[@"formatted_address"]); }); #endif } } /* { "status":"OK", "result":{ "location":{ "lng":113.675911, "lat":34.772749 }, "formatted_address":"河南省鄭州市金水區經八路26號", "business":"勝崗,經八路,金水路", "addressComponent":{ "city":"鄭州市", "direction":"near", "distance":"12", "district":"金水區", "province":"河南省", "street":"經八路", "street_number":"26號" }, "cityCode":268 } } */ - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { NSLog(@"定位失敗"); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end