知识点:
1.CoreLocation框架—负责定位的(手机的)位置。用于地理定位
2.MapKit.framework—系统自带的框架,地图展示,
3.ios7 中:系统会自动申请用户位置权限。
CoreLocation中使用
CLLocationManager对象来做用户定位,
CoreLocation框架中所有数据类型的前缀都是
CL
定位的类(位置管理器最好懒加载创建,强引用):CLLocationManager ,用alloc init,创建 ,监听用代理,实现代理方法, 开始更新用户的位置【位置管理器的对象 startUpdatingLocation】,
用来表示经纬度的结构体——CLLocationCoordinate2D
latitude —纬度 longitude — 经度 course —朝向 timeStamp —更新的点的时间, 测试楼层--floor
4.ios8之后,要做一个判断,如果是,要申请用户权限。代理方法---判断授权状态,status 在Info。plist文件中配置Key值,(当用户在前台还是后台的时候。 请求数据 request。。。。)---代理方法---判断授权状态,
5.调整位置的经纬度—deBug 中的location;
6.位置管理器的常见属性
当用户移动了多长的距离之后,才更新用户的位置(米),对象.distanceFilter =10;
定位精度(精度越高越费电):对象.desiredAccuracy =
kCLLocationAccuracyBestForNavigation;
extern const CLLocationAccuracy kCLLocationAccuracyBestForNavigation;
导航使用最好进度
// extern const CLLocationAccuracy kCLLocationAccuracyBest;
最高精度
// extern const CLLocationAccuracy kCLLocationAccuracyNearestTenMeters; 10M
// extern const CLLocationAccuracy kCLLocationAccuracyHundredMeters; 100M
// extern const CLLocationAccuracy kCLLocationAccuracyKilometer; 1000M
// extern const CLLocationAccuracy kCLLocationAccuracyNearestTenMeters; 10M
// extern const CLLocationAccuracy kCLLocationAccuracyHundredMeters; 100M
// extern const CLLocationAccuracy kCLLocationAccuracyKilometer; 1000M
// extern const CLLocationAccuracy kCLLocationAccuracyThreeKilometers; 3000M
导航的使用场景要用activityType =定位数据的用途
self.
locationManager.
activityType =
CLActivityTypeFitness;
// CLActivityTypeOther = 1,
// CLActivityTypeAutomotiveNavigation, // 汽车导航
// CLActivityTypeFitness, // 步行导航
// CLActivityTypeOtherNavigation // 其他导航 , 比如轮船 , 火车 , 飞机
// CLActivityTypeOther = 1,
// CLActivityTypeAutomotiveNavigation, // 汽车导航
// CLActivityTypeFitness, // 步行导航
// CLActivityTypeOtherNavigation // 其他导航 , 比如轮船 , 火车 , 飞机
//
CLLocation用来表示某个位置的地理信息,比如经纬度、海拔等等。常见属性
CLLocationCoordinate2D coordinate;表示经纬度
CLLocationDistance altitude;表示海拔
CLLocationDirection course;表示路线,航向(取值范围是0.0° ~ 359.9°,0.0°代表真北方向)
CLLocationSpeed speed;表示行走速度(单位是m/s)
- 用- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location方法可以计算2个位置之间的距离
- 计算两个时间点之间的时间差的方法: self.sumTime += [currentLocation.timestamp timeIntervalSinceDate:self.preLocation.timestamp];
计算平均速度案例:
- (
void)locationManager:(
CLLocationManager *)manager didUpdateLocations:(
NSArray<
CLLocation *> *)locations
{
{
CLLocation *currentLocation = locations.
lastObject;
if ( self. preLocation == nil) {
self. preLocation = currentLocation;
return;
}
// 计算地图上两点之间的距离 , 以米为单位的
self.
sumDistance += [currentLocation
distanceFromLocation:
self.
preLocation];
self.
sumTime += [currentLocation.
timestamp
timeIntervalSinceDate:
self.
preLocation.
timestamp];
// 计算平均速度
CGFloat avgSpeed = self. sumDistance / self. sumTime;
NSLog(
@"
总距离
:%lf,
总时间
:%lf,
平均速度
:%lf",
self.
sumDistance,
self.
sumTime,avgSpeed);
}
CLHeading:用来表示方向相关的数据
//
更新用户的方向
[
self.
locationManager
startUpdatingHeading];
- @property(readonly, nonatomic) CLLocationDirection magneticHeading
- 磁北偏移量, 0度表示磁北, 0 – 359.9
- 磁北: 磁场北极,是指南针所指的北
- @property(readonly, nonatomic) CLLocationDirection trueHeading
- 真北偏移量, 0度表示真北
- 真北:地理北极, 是地图或者地球仪上所有经线的起始点
设置指南针案例:
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface
ViewController ()<
CLLocationManagerDelegate>
@property (
weak,
nonatomic)
IBOutlet
UIImageView *compass;指南针
@property(
nonatomic,
strong)
CLLocationManager * locationManager ;
@end
@implementation ViewController
#pragma mark - 懒加载 要在真机上试
-( CLLocationManager *)locationManager{
if (! _locationManager) {
_locationManager = [[ CLLocationManager alloc] init];
_locationManager. delegate = self;
}
return _locationManager;
@end
@implementation ViewController
#pragma mark - 懒加载 要在真机上试
-( CLLocationManager *)locationManager{
if (! _locationManager) {
_locationManager = [[ CLLocationManager alloc] init];
_locationManager. delegate = self;
}
return _locationManager;
}
- (
void)viewDidLoad {
[
super
viewDidLoad];
iOS开发指南:利用CoreLocation和MapKit实现精准定位与地图展示

本文深入探讨iOS开发中如何运用CoreLocation框架进行精准定位,以及如何通过MapKit框架展示地图。重点介绍如何在iOS应用中获取和处理用户位置信息,包括定位权限管理、位置更新机制、位置数据类型解析,以及如何使用MapKit绘制地图和添加地图标注。此外,文章还讲解了如何调整地图显示区域、设置用户跟踪模式、添加自定义地图标注,并通过地理编码与反地理编码实现地点信息的转换。最后,提供实例代码演示核心功能的应用,帮助开发者快速掌握关键技巧。
最低0.47元/天 解锁文章
4796

被折叠的 条评论
为什么被折叠?



