1.实现定位功能需要导入系统库MapKit.framework

2.在iPhone手机上默认是禁止手机定位的,所以,要询问系统是否开启手机定位功能。

为了开启手机定位功能,还需在info.plist中添加开启定位描述。如图,

3.功能代码如下:

  1. //
  2. // ViewController.m
  3. // MapLocationDemo
  4. //
  5. // Created by apple on 15/12/3.
  6. // Copyright © 2015年 hoondraw. All rights reserved.
  7. //
  8.  
  9. #import "ViewController.h"
  10. #import <CoreLocation/CoreLocation.h>
  11. #import "MapViewController.h"
  12.  
  13. @interface ViewController () <CLLocationManagerDelegate>
  14.  
  15. /** 定位管理类*/
  16. @property (strong, nonatomic) CLLocationManager *mgr;
  17.  
  18. /** 地理编码对象*/
  19. @property (strong, nonatomic) CLGeocoder *geocoder;
  20.  
  21. @property (weak, nonatomic) IBOutlet UILabel *latLabel;
  22.  
  23. @property (weak, nonatomic) IBOutlet UILabel *longLabel;
  24.  
  25. @property (weak, nonatomic) IBOutlet UILabel *geocoderLabel;
  26.  
  27. /** 当前定位信息*/
  28. @property (strong, nonatomic) CLLocation *currentLocation;
  29.  
  30. @end
  31.  
  32. @implementation ViewController
  33.  
  34. - (CLLocationManager *)mgr {
  35. if (!_mgr) {
  36. _mgr = [[CLLocationManager alloc] init];
  37. }
  38. return _mgr;
  39. }
  40.  
  41. - (CLGeocoder *)geocoder {
  42. if (!_geocoder) {
  43. _geocoder = [CLGeocoder new];
  44. }
  45. return _geocoder;
  46. }
  47.  
  48. - (void)viewDidLoad {
  49. [super viewDidLoad];
  50. self.mgr.delegate = self;
  51.  
  52. if ([[UIDevice currentDevice].systemVersion doubleValue]> 8.0) {
  53. //只在前台定位
  54. [self.mgr requestWhenInUseAuthorization];
  55. } else {
  56. NSLog(@"iOS7");
  57. //直接开始定位
  58. [self.mgr startUpdatingLocation];
  59. }
  60. }
  61.  
  62. #pragma mark - 定位协议方法
  63. - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
  64. if (status == kCLAuthorizationStatusAuthorizedWhenInUse) {
  65. //定位的设置参数
  66. self.mgr.desiredAccuracy = kCLLocationAccuracyBest;
  67. self.mgr.distanceFilter = kCLHeadingFilterNone;
  68. [self.mgr startUpdatingLocation];
  69. } else {
  70. //不允许定位
  71. }
  72.  
  73. }
  74.  
  75. - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
  76. CLLocation *location = [locations lastObject];
  77. self.latLabel.text = [NSString stringWithFormat:@"经度:%lf", location.coordinate.latitude];
  78. self.longLabel.text = [NSString stringWithFormat:@"纬度:%lf", location.coordinate.longitude];
  79.  
  80. //获取当前的location
  81. self.currentLocation = location;
  82.  
  83. [self.mgr stopUpdatingLocation];
  84. }
  85. /** 地理编码*/
  86. - (IBAction)clickGecoder:(id)sender {
  87. NSString *name = @"广州市天河区天河珠江新城";
  88. //从服务器获取name对应的经纬度(请求)
  89. [self.geocoder geocodeAddressString:name completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
  90. if (!error) {
  91. //地理编码成功
  92. for (CLPlacemark *placemark in placemarks) {
  93. self.latLabel.text = [NSString stringWithFormat:@"纬度:%lf",placemark.location.coordinate.latitude];
  94. self.longLabel.text = [NSString stringWithFormat:@"经度:%lf",placemark.location.coordinate.longitude];
  95. self.geocoderLabel.text = [NSString stringWithFormat:@"地址信息:%@",placemark.addressDictionary];
  96. }
  97. }
  98. }];
  99. }
  100.  
  101. /** 反地理编码*/
  102. - (IBAction)clickGeoDecoder:(id)sender {
  103. CLLocation *location = [[CLLocation alloc] initWithLatitude:self.currentLocation.coordinate.latitude longitude:self.currentLocation.coordinate.longitude];
  104.  
  105. [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
  106. if (!error) {
  107. //反地理编码成功
  108. for (CLPlacemark *placemark in placemarks) {
  109. self.latLabel.text = [NSString stringWithFormat:@"纬度:%lf",placemark.location.coordinate.latitude];
  110. self.longLabel.text = [NSString stringWithFormat:@"经度:%lf",placemark.location.coordinate.longitude];
  111. self.geocoderLabel.text = [NSString stringWithFormat:@"地址信息:%@",placemark.addressDictionary];
  112. }
  113. }
  114. }];
  115. }
  116.  
  117. - (IBAction)gotoMapView:(id)sender {
  118. MapViewController *vc = [[MapViewController alloc] init];
  119. [self presentViewController:vc animated:YES completion:nil];
  120. }
  121.  
  122. - (void)didReceiveMemoryWarning {
  123. [super didReceiveMemoryWarning];
  124. }

其中,gotoMapView是跳转到地图控制器的方法。

代码如下:

  1. //
  2. // MapViewController.m
  3. // MapLocationDemo
  4. //
  5. // Created by apple on 15/12/3.
  6. // Copyright © 2015年 hoondraw. All rights reserved.
  7. //
  8.  
  9. #import "MapViewController.h"
  10. #import <MapKit/MapKit.h>
  11.  
  12. @interface MapViewController () <MKMapViewDelegate>
  13.  
  14. @property (weak, nonatomic) IBOutlet MKMapView *mapView;
  15.  
  16. @property (strong, nonatomic) CLLocationManager *mgr;
  17.  
  18. @end
  19.  
  20. @implementation MapViewController
  21.  
  22. - (void)viewDidLoad {
  23. [super viewDidLoad];
  24.  
  25. self.mgr = [[CLLocationManager alloc] init];
  26. [self.mgr requestWhenInUseAuthorization];
  27. //设置和地图相关的属性
  28. self.mapView.delegate = self;
  29. //设置地图的旋转属性
  30. self.mapView.rotateEnabled = YES;
  31. //使用地图视图开始定位
  32. self.mapView.userTrackingMode = MKUserTrackingModeFollow;
  33.  
  34. }
  35.  
  36. #pragma mark - MKMapViewDelegate
  37. - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
  38. //地图显示的区域
  39. //设置地图的中心
  40. CLLocationCoordinate2D center = userLocation.location.coordinate;
  41. //设置跨度
  42. MKCoordinateSpan span = MKCoordinateSpanMake(, );
  43. MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
  44. [self.mapView setRegion:region animated:YES];
  45. }
  46.  
  47. - (void)didReceiveMemoryWarning {
  48. [super didReceiveMemoryWarning];
  49. }
  50.  
  51. @end

这样就可以跳转到地图中显示当前地理位置。

iOS定位功能的更多相关文章

  1. iOS 定位功能的实现

    1.导入框架 Xcode中添加"CoreLocation.framework" 2.导入主头文件 #import <CoreLocation/CoreLocation.h&g ...

  2. Flex AIR应用GPS定位功能(Android和IOS)

    说明: 使用AIR进行GPS定位功能实现时,会经常判断GPS是否打开.一般的官方或者书上的介绍的方法,测试后,只能对Android系统进行判断,而对ios系统则无法进行判断. 经过研究测试,终于解决实 ...

  3. iOS 设备定位功能可用的判断

    if ([CLLocationManager locationServicesEnabled] && ([CLLocationManager authorizationStatus] ...

  4. ios 定位

    ios 定位新功能----在程序中实现定位功能 Core Location是iOS SDK中一个提供设备位置的框架.可以使用三种技术来获取位置:GPS.蜂窝或WiFi.在这些技术中,GPS最为精准,如 ...

  5. ionic 添加地图定位功能

    由于项目需求,需要一个定位功能,通过google或百度,搜到一个cordova-plugin-geolocation的插件,在ios上可以用,但是在android就呵呵了,原因就不说了,大家都知道.所 ...

  6. IOS定位服务的应用

    IOS定位服务的应用 一.授权的申请与设置 二.定位服务相关方法 三.定位服务代理的相关方法 四.定位服务获取到的位置对象 五.航标定位得到的航标信息对象 IOS定位服务的应用 一.授权的申请与设置 ...

  7. 转载]IOS LBS功能详解[0](获取经纬度)[1](获取当前地理位置文本 )

    原文地址:IOS LBS功能详解[0](获取经纬度)[1](获取当前地理位置文本作者:佐佐木小次郎 因为最近项目上要用有关LBS的功能.于是我便做一下预研. 一般说来LBS功能一般分为两块:一块是地理 ...

  8. 对于WIFI版ipad(无GPS芯片)定位功能的释疑

    把玩ipad(WIFI版ipad,无GPS芯片)很久时间了,曾今有很多人(包括我)也用过它的定位功能,发现它确实很准确,通常的误差在40米以内,所以很多人都怀疑这个版本的ipad是不是真的内置了GPS ...

  9. iOS-系统定位功能

    ios系统定位 前期准备 系统定位功能,需要用到框架:CoreLocation/CoreLocation.h, 然后导入文件#import <CoreLocation/CoreLocation. ...

随机推荐

  1. [Django] Windows 下安装 配置Pinax 工程

    Pinax 是一个基于Django开发的脚手架,有一些现成的模板和功能模块可以使用,方便快速有效的开发一个Django项目.下面举个例子如何安装一个pinax项目到集成开发环境Aptana里面. 先从 ...

  2. Unity3d GUI弹窗

    ArrayList w_position = new ArrayList(); void OnGUI() { if (GUILayout.Button("Open")) { if ...

  3. network is unreachable 解决方案之一

    请按http://blog.csdn.net/oxiaoxio/article/details/41775183 一文中的设置方法设置:

  4. 最简单的视音频播放演示样例4:Direct3D播放RGB(通过Texture)

    ===================================================== 最简单的视音频播放演示样例系列文章列表: 最简单的视音频播放演示样例1:总述 最简单的视音频 ...

  5. sql中在查询语句中加判断,控制输出的内容

    Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END ...

  6. javascript高级知识分析——实例化

    代码信息来自于http://ejohn.org/apps/learn/. new做了什么? function Ninja(){ this.name = "Ninja"; } var ...

  7. linux新建磁盘并分区

    先在虚拟机上添加一块硬盘. 查看磁盘分区:sdb还没有分配 新建一个100M的分区:再查看,发现新建成功了. 再查看fdisk -l ******ext4格式不支持.就使用了ext2进行格式化了. m ...

  8. hadoop 学习

    不同版本间Hadoop拷贝 通过NFS,将hdfs挂在到本地

  9. 查看EBS R12应用中使用CONTEXT_FILE的版本及路径

    SELECT * FROM APPLSYS.FND_OAM_CONTEXT_FILES;

  10. 开源一个监控数据采集Agent:OpenFalcon-SuitAgent

    OpenFalcon-SuitAgent 项目地址:github 版本说明 本系统版本划分如下 alpha:内部测试版(不建议使用于生产环境) beta:公开测试版(不建议使用于生产环境) final ...