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

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

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

3.功能代码如下:

//
// ViewController.m
// MapLocationDemo
//
// Created by apple on 15/12/3.
// Copyright © 2015年 hoondraw. All rights reserved.
// #import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
#import "MapViewController.h" @interface ViewController () <CLLocationManagerDelegate> /** 定位管理类*/
@property (strong, nonatomic) CLLocationManager *mgr; /** 地理编码对象*/
@property (strong, nonatomic) CLGeocoder *geocoder; @property (weak, nonatomic) IBOutlet UILabel *latLabel; @property (weak, nonatomic) IBOutlet UILabel *longLabel; @property (weak, nonatomic) IBOutlet UILabel *geocoderLabel; /** 当前定位信息*/
@property (strong, nonatomic) CLLocation *currentLocation; @end @implementation ViewController - (CLLocationManager *)mgr {
if (!_mgr) {
_mgr = [[CLLocationManager alloc] init];
}
return _mgr;
} - (CLGeocoder *)geocoder {
if (!_geocoder) {
_geocoder = [CLGeocoder new];
}
return _geocoder;
} - (void)viewDidLoad {
[super viewDidLoad];
self.mgr.delegate = self; if ([[UIDevice currentDevice].systemVersion doubleValue]> 8.0) {
//只在前台定位
[self.mgr requestWhenInUseAuthorization];
} else {
NSLog(@"iOS7");
//直接开始定位
[self.mgr startUpdatingLocation];
}
} #pragma mark - 定位协议方法
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
if (status == kCLAuthorizationStatusAuthorizedWhenInUse) {
//定位的设置参数
self.mgr.desiredAccuracy = kCLLocationAccuracyBest;
self.mgr.distanceFilter = kCLHeadingFilterNone;
[self.mgr startUpdatingLocation];
} else {
//不允许定位
} } - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
CLLocation *location = [locations lastObject];
self.latLabel.text = [NSString stringWithFormat:@"经度:%lf", location.coordinate.latitude];
self.longLabel.text = [NSString stringWithFormat:@"纬度:%lf", location.coordinate.longitude]; //获取当前的location
self.currentLocation = location; [self.mgr stopUpdatingLocation];
}
/** 地理编码*/
- (IBAction)clickGecoder:(id)sender {
NSString *name = @"广州市天河区天河珠江新城";
//从服务器获取name对应的经纬度(请求)
[self.geocoder geocodeAddressString:name completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if (!error) {
//地理编码成功
for (CLPlacemark *placemark in placemarks) {
self.latLabel.text = [NSString stringWithFormat:@"纬度:%lf",placemark.location.coordinate.latitude];
self.longLabel.text = [NSString stringWithFormat:@"经度:%lf",placemark.location.coordinate.longitude];
self.geocoderLabel.text = [NSString stringWithFormat:@"地址信息:%@",placemark.addressDictionary];
}
}
}];
} /** 反地理编码*/
- (IBAction)clickGeoDecoder:(id)sender {
CLLocation *location = [[CLLocation alloc] initWithLatitude:self.currentLocation.coordinate.latitude longitude:self.currentLocation.coordinate.longitude]; [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if (!error) {
//反地理编码成功
for (CLPlacemark *placemark in placemarks) {
self.latLabel.text = [NSString stringWithFormat:@"纬度:%lf",placemark.location.coordinate.latitude];
self.longLabel.text = [NSString stringWithFormat:@"经度:%lf",placemark.location.coordinate.longitude];
self.geocoderLabel.text = [NSString stringWithFormat:@"地址信息:%@",placemark.addressDictionary];
}
}
}];
} - (IBAction)gotoMapView:(id)sender {
MapViewController *vc = [[MapViewController alloc] init];
[self presentViewController:vc animated:YES completion:nil];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}

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

代码如下:

//
// MapViewController.m
// MapLocationDemo
//
// Created by apple on 15/12/3.
// Copyright © 2015年 hoondraw. All rights reserved.
// #import "MapViewController.h"
#import <MapKit/MapKit.h> @interface MapViewController () <MKMapViewDelegate> @property (weak, nonatomic) IBOutlet MKMapView *mapView; @property (strong, nonatomic) CLLocationManager *mgr; @end @implementation MapViewController - (void)viewDidLoad {
[super viewDidLoad]; self.mgr = [[CLLocationManager alloc] init];
[self.mgr requestWhenInUseAuthorization];
//设置和地图相关的属性
self.mapView.delegate = self;
//设置地图的旋转属性
self.mapView.rotateEnabled = YES;
//使用地图视图开始定位
self.mapView.userTrackingMode = MKUserTrackingModeFollow; } #pragma mark - MKMapViewDelegate
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
//地图显示的区域
//设置地图的中心
CLLocationCoordinate2D center = userLocation.location.coordinate;
//设置跨度
MKCoordinateSpan span = MKCoordinateSpanMake(, );
MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
[self.mapView setRegion:region animated:YES];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
} @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. 通宵疯狂积累VB.NET基础知识

    VB.NET中Module的概念 为什么VB.NET中会有一个Module的东西,而在C#等语言中是没有的 首先,这是一个历史原因.早先的VB语言都有模块和类模块的概念.所谓模块一般就是存放公用的一些 ...

  2. Session和Cookie的关系

    Session和Cookie关系 两者构建了web的回话数据 Cookie作为客户端的回话,Session为服务器端的 共同点: 都是1对1的,(一个客户一个独立的回话) 都以键值对的方式存储数据 都 ...

  3. Python urllib和urllib2模块学习(三)

    build_opener()详解: 1.urllib2.urlopen()函数不支持验证.cookie或者其它HTTP高级功能,要支持这些功能,必须使用build_opener()函数创建自定这句话的 ...

  4. to_date如何处理毫秒?

    http://blog.csdn.net/jamex/archive/2008/09/08/2899172.aspx to_date如何处理毫秒? 如把"1970-01-01 00:00:0 ...

  5. SPI模式下MCU对SD卡的控制及操作命令

    一.前言 SD 卡有两个可选的通讯协议:SD 模式和 SPI模式 SD 模式是SD 卡标准的读写方式,但是在选用SD 模式时,往往需要选择带有SD 卡控制器接口的 MCU,或者必须加入额外的SD卡控制 ...

  6. 8 个优秀的 Linux 图形图像及色彩工具

    8 个优秀的 Linux 图形图像及色彩工具 1. 硬件色彩分析器LPROF LPROF 是一个用于创建设备兼容,如相机.扫描仪.显示器的ICC兼容型材的颜色分析器.这些配置提供跨设备的色彩一致性.他 ...

  7. C语言对象化编程

    以下为一个引子: C中struct的函数实现,只能用函数指针成员. C结构体内不能有函数的代码,但可以有函数的指针. C/C code Code highlighting produced by Ac ...

  8. [UI]抽屉菜单DrawerLayout分析(二)

    继续分析DrawerLayout的手势分发部分 谈到手势分发,这本身就是个好话题,DrawerLayout作为继承自ViewGroup得布局他可以拦截手势也可以分发给子view,也就是在 onInte ...

  9. java 解析json的问题

    本文转载自http://chriszz.sinaapp.com/?p=392 Json就是Javascript notation,可以替代XML,用做数据交互. Json的两种基本表示形式,可以用自动 ...

  10. 《think in python》学习-6

    think in python 有返回函数 我们使用过的内置函数中,有一部分会返回结果,比如 math的 返回值 我们写一个有返回值的函数,计算给定半径的圆的面积,例如这个: def area(rad ...