1. CoreLocation定位框架的使用:
手机定位有三种方式:手机基站(耗电量少,定位速度快,但定位误差大),GPS(定位误差小,但耗电量大,定位速度慢),WIFI(介于两者之间)。
/*
创建工程后需要在info文件中增加两个系统配置文件,这两个文件会向用户请求获取用户位置信息
<key>NSLocationWhenInUseUsageDescription</key>
<string>此处的Value值由开发者自定义</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>此处的Value值由开发者自定义</string>
*/
#import "ViewController.h"
//导入获取位置框架
#import <CoreLocation/CoreLocation.h>
@interface ViewController () <CLLocationManagerDelegate>
//注意:必须将CLLocationManager对象定义成属性,否则授权获取位置窗口会出现闪退。
@property (nonatomic, strong)CLLocationManager *locationManager;
@end
@implementation ViewController
@synthesize locationManager;
- (void)viewDidLoad {
[super viewDidLoad];
//判断定位服务是否可用
if([CLLocationManager locationServicesEnabled])
{
//注意:必须将CLLocationManager对象定义成属性,否则授权获取位置窗口会出现闪退。
//创建一个定位管理对象,负责获取定位信息
locationManager = [[CLLocationManager alloc] init];
//设置定位精度
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
//根据移动距离更新位置
locationManager.distanceFilter = 40;
//认证NSLocationAlwaysUsageDescription
if([[UIDevice currentDevice] systemVersion].doubleValue>8.0)//如果ios是8.0以上版本
{
if([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])//位置管理对象中有requestAlwaysAuthorization这个行为
{
//向用户请求定位授权
[locationManager requestAlwaysAuthorization];
}
}
//判断当前设备是否可以进行方向检测
if([CLLocationManager headingAvailable])
{
//开始更新方向
[locationManager startUpdatingHeading];
}
else
{
NSLog(@"当前设备不支持磁力计");
}
//对一个区域进行检测,当设备进入次区域时发出通知
CLLocationCoordinate2D roundCenter;
roundCenter.latitude = 37.3232;
roundCenter.longitude = 22.43434;
//创建一个圆形区域
CLRegion *region = [[CLCircularRegion alloc] initWithCenter:roundCenter radius:100 identifier:@"iOS"];
//开始监听该区域
[locationManager startMonitoringForRegion:region];
//设置代理
locationManager.delegate = self;
//开始更新定位信息
[locationManager startUpdatingLocation];
//如果不需要持续跟踪用户的行踪,定位之后,为节省用户电量,用stopUpdatingLocation结束定位。
}
else
{
NSLog(@"无法使用定位系统");
}
}
#pragma mark 获取定位需要用到的代理方法
//定位获取成功后激活该方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
NSLog(@"定位获取成功!--%@",locations);
//location对象包含的属性有:
//coordinate: 该属性是个结构体,返回经度和纬度信息
//altitude: 该属性表示海拔高度/单位m
//horizontalAccuracy: 该属性表示定位信息的水平精度,经纬坐标视为圆心,水平精度视为半径。精度值越小,定位精度越高,精度为负,表示精度测量失败。
//verticalAccuray: 该属性表示定位信息的垂直精度。
//speed: 该属性返回当前设备的移动速度。单位:m/s。
//course: 该属性返回设备移动的方向。course的值是0-360之间的浮点数,0表示正北方向,90表示正东方向,180表示正南方向,270表示正西方向。
//timestamp: 该属性返回定位信息的定位时间。
}
//定位失败后激活该方法
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"定位失败--%@",error);
//失败错误信息:
//Code=0说明没有位置信息,即没获取到GPS信息
//Code=1说明是系统授权问题,尤其是iOS8
}
#pragma mark 方向检测需要用到的代理方法
//获取设备方向后激活该方法
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
//CLHeading对象还包含属性
//headingAccuracy 精度
//timestamp 测量时间读取
//description 描述
//trueHeading 表示真实方向
//magneticHeading 表示磁性航向
//将设备的方向角度转换成弧度。
CGFloat headings = -0.1f * M_PI * newHeading.magneticHeading / 180.0f;
NSLog(@"设备方向的弧度值为%f",headings);
}
//对位置管理器进行校准
- (BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)manager
{
return YES;
}
#pragma mark 区域检测需要用到的代理方法
//当设备进入指定区域后调用该方法
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
NSLog(@"设备已经入指定区域");
}
//当设备离开指定区域后调用该方法
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
NSLog(@"设备已经离开指定区域");
}
2. MapKit地图框架的使用
import "ViewController.h"
import <MapKit/MapKit.h>
import <CoreLocation/CoreLocation.h>
@interface ViewController () <MKMapViewDelegate>
@property (nonatomic, strong)CLLocationManager *locationManager;
@property (nonatomic, strong)MKMapView *myMapView;
@property (nonatomic, strong)CLGeocoder *geocoder;
@end
@implementation ViewController
@synthesize myMapView,geocoder;
- (void)viewDidLoad {
[super viewDidLoad];
//请求用户允许访问用户位置
_locationManager = [[CLLocationManager alloc] init];
[_locationManager requestAlwaysAuthorization];
//添加地图
[self addMapView];
//根据地点名称在地图上定位,并添加锚点
[self addMapLocationWithNSStrong:@"天安门"];
//实例化一个手势,长按手势在地图上添加锚点
UILongPressGestureRecognizer *press = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(addAnchorPoint:)];
[myMapView addGestureRecognizer:press];
}
- (void)addMapView
{
myMapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:myMapView];
//设置地图的现实风格
/*
MKMapTypeStandard = 0, 标准类型
MKMapTypeSatellite, 卫星类型
MKMapTypeHybrid, 混合地图
*/
myMapView.mapType = MKMapTypeStandard;
/*
地图跟踪模式
MKUserTrackingModeNone = 0 不跟踪用户位置
MKUserTrackingModeFollow 跟踪用户位置
MKUserTrackingModeFollowWithHeading 带方向跟踪用户位置
*/
myMapView.userTrackingMode = MKUserTrackingModeFollow;
//允许地图进行缩放
myMapView.zoomEnabled = YES;
//允许地图进行滚动
myMapView.scrollEnabled = YES;
//允许地图进行旋转
myMapView.rotateEnabled = YES;
//地图显示用户当前位置
myMapView.showsUserLocation = YES;
//设置代理
myMapView.delegate = self;
}
- (void)addMapLocationWithNSStrong:(NSString *)name
{
//CLGeocoder类可以将字符串转换成经纬度,也可以将经纬度转换成字符串地址。
geocoder = [[CLGeocoder alloc] init];
//将字符串转换成经纬度。
[geocoder geocodeAddressString:name completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
//如果发现placemarks中包含多个元素,根据此字符串检索出多个位置。
if(placemarks.count > 0 && error == nil)
{
//placemarks包含的常用属性:
//location: 返回一个CLLocation结构体
//name: 返回placemarks指向的地址名称
//可以用placemarks包含的其他属性获取一个地方的具体地址
//设置地图中心的经纬度
CLLocationCoordinate2D center = {placemarks[0].location.coordinate.latitude,placemarks[0].location.coordinate.longitude};
//设置地图的显示范围,范围越小,细节越清楚。
MKCoordinateSpan span = {0.01, 0.01};
//region对象表示地图的中心点和显示范围。
MKCoordinateRegion region = {center, span};
[myMapView setRegion:region animated:YES];
//实例化一个锚点
MKPointAnnotation *anchorpPoint = [[MKPointAnnotation alloc] init];
//设置锚点的经纬坐标
anchorpPoint.coordinate = placemarks[0].location.coordinate;
//设置锚点的标题
anchorpPoint.title = placemarks[0].name;
//将锚点添加到地图上
[myMapView addAnnotation:anchorpPoint];
}
else
{
NSLog(@"定位失败");
}
}];
}
//长按手势在地图上添加锚点
- (void)addAnchorPoint:(UILongPressGestureRecognizer *)press
{
if (UIGestureRecognizerStateBegan == press.state)
{
CGPoint point = [press locationInView:self.myMapView];
CLLocationCoordinate2D coordinate = [self.myMapView convertPoint:point toCoordinateFromView:self.myMapView];
//设置地图的显示范围,范围越小,细节越清楚。
MKCoordinateSpan span = {0.01, 0.01};
//region对象表示地图的中心点和显示范围。
MKCoordinateRegion region = {coordinate, span};
[myMapView setRegion:region animated:YES];
//实例化一个锚点
MKPointAnnotation *anchorPoint = [[MKPointAnnotation alloc] init];
//设置锚点的经纬坐标
anchorPoint.coordinate = coordinate;
//将锚点添加到地图上
[myMapView addAnnotation:anchorPoint];
}
}
//MKAnnotationView继承于UIView,该方法返回的是地图上的锚点控件,重写该方法可以自己定制锚点的图片
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
static NSString *identifier = @"aa";
//获得可重用的锚点控件
MKAnnotationView *anchorPoint = [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
//如果锚点控件不存在,则创建新的可重用锚点
if(anchorPoint == nil)
{
anchorPoint = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
}
//提示:如果是自定义大头针视图,需要设置canShowCallout属性,才能够和视图进行交互
anchorPoint.image = [UIImage imageNamed:@"del_click"];
return anchorPoint;
}