免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1466 | 回复: 0
打印 上一主题 下一主题

[iOS] iOS 8 实现获取当前定位信息 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-06-30 09:46 |只看该作者 |倒序浏览
iOS 8 实现获取当前定位信息

源码:https://git.oschina.net/laughingzhong/LocationDemo.git

获取当前定位信息代码
  1. //
  2. //  ViewController.m
  3. //  LocationDemo
  4. //
  5. //  Created by LaughingZhong on 14/11/12.
  6. //  Copyright (c) 2014年 Laughing. All rights reserved.
  7. //

  8. #import "ViewController.h"

  9. @interface ViewController ()

  10. @end

  11. @implementation ViewController
  12. @synthesize myLocationManager,myGeocoder,myLocation;

  13. - (void)dealloc
  14. {
  15.     self.myLocationManager = nil;
  16.     self.myLocation = nil;
  17.     self.myGeocoder = nil;
  18. }

  19. - (void)viewDidLoad {
  20.     [super viewDidLoad];
  21.     // Do any additional setup after loading the view from its nib.
  22.     if ([CLLocationManager locationServicesEnabled]) {
  23.         self.myLocationManager = [[CLLocationManager alloc] init];
  24.         [self.myLocationManager setDelegate:self];
  25.         self.myLocationManager.desiredAccuracy = kCLLocationAccuracyBest;
  26.         self.myLocationManager.distanceFilter = 1.0;
  27. //        [self.myLocationManager requestAlwaysAuthorization];
  28.         [self.myLocationManager requestWhenInUseAuthorization];
  29.         [self.myLocationManager startUpdatingLocation];
  30.     }else {
  31.         NSLog(@"Location services are not enabled");
  32.     }
  33. }

  34. - (void)didReceiveMemoryWarning {
  35.     [super didReceiveMemoryWarning];
  36.     // Dispose of any resources that can be recreated.
  37. }

  38. #pragma mark -
  39. #pragma mark CLLocationManagerDelegate
  40. - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
  41. {
  42.     NSLog(@"locations is %@",locations);
  43.     CLLocation *location = [locations lastObject];
  44.     self.myGeocoder = [[CLGeocoder alloc] init];
  45.     [self.myGeocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error)
  46.      {
  47.          if(error == nil && [placemarks count]>0)
  48.          {
  49.              CLPlacemark *placemark = [placemarks objectAtIndex:0];
  50.               
  51.              NSLog(@"name = %@",placemark.name);
  52.              NSLog(@"Country = %@", placemark.country);
  53.              NSLog(@"Postal Code = %@", placemark.postalCode);
  54.              NSLog(@"locality = %@", placemark.locality);
  55.              NSLog(@"subLocality = %@", placemark.subLocality);
  56.              NSLog(@"address = %@",placemark.name);
  57.              NSLog(@"administrativeArea = %@",placemark.administrativeArea);
  58.              NSLog(@"subAdministrativeArea = %@",placemark.subAdministrativeArea);
  59.              NSLog(@"ISOcountryCode = %@",placemark.ISOcountryCode);
  60.              NSLog(@"thoroughfare = %@", placemark.thoroughfare);
  61.              NSLog(@"subThoroughfare = %@",placemark.subThoroughfare);
  62.               
  63.              [label setText:[NSString stringWithFormat:@"address is: %@",placemark.name]];
  64.          }
  65.          else if(error==nil && [placemarks count]==0){
  66.              NSLog(@"No results were returned.");
  67.          }
  68.          else if(error != nil) {
  69.              NSLog(@"An error occurred = %@", error);
  70.          }
  71.      }];
  72.     [self.myLocationManager stopUpdatingLocation];
  73. }

  74. - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
  75. {
  76.     NSLog(@"reverse geocoder error: %@", [error description]);
  77. }


  78. @end
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP