Core Location和MapKit的一些简单使用

本文详细介绍了iOS开发中地理位置定位(CoreLocation)及地图显示(MapKit)的技术要点,包括CLLocationManager的配置与使用、不同位置服务的特点、地理编码实现、MKMapView的定制化展示等。

Core Location

1. 基本对象是CLLocation,有属性coordinate, altitude, horizontal/vertical Accuracy, timestamp, speed, course

typedef {
CLLocationDegrees latitude; // a double
CLLocationDegrees longitude; // a double 
} CLLocationCoordinate2D;    // 经纬度

@property (readonly) CLLocationAccuracy horizontalAccuracy; // in meters 
@property (readonly) CLLocationAccuracy verticalAccuracy;

kCLLocationAccuracyBestForNavigation;
kCLLocationAccuracyBest;
kCLLocationAccuracyNearestTenMeters;
kCLLocationAccuracyHundredMeters;
kCLLocationAccuracyKilometer;
kCLLocationAccuracyThreeKilometers;


- (CLLocationDistance)distanceFromLocation:(CLLocation *)otherLocation; // in meters 两个位置之间的距离


2. 总是通过CLLocationManager来获取CLLocation(通过其代理),其一般的使用方法为:

(1)检查硬件是否支持你需要的位置更新

(2)创建一个CLLocationManager实例,并设置接收更新的代理对象

(3)根据你的需求对CLLocationManager进行配置

(4)启动CLLocationManager来监视改变。


3. Core Location Manager的一些设置

@property CLLocationAccuracy desiredAccuracy; // always set this as low as possible 
@property CLLocationDistance distanceFilter; 

- (void)startUpdatingLocation;
- (void)stopUpdatingLocation;

- (void)startMonitoringSignificantLocationChanges;
- (void)stopMonitoringSignificantLocationChanges;


Core Location框架提供了三种用于追踪设备当前位置的服务

  • The significant-change location 

    service 提供了低耗电的方法来获取当前位置,当前位置改变时会发出通知
  • The standard location service 提供了一种可设置的方法来获取当前位置

  • Region monitoring 监视特定地区的跨越

Listing 1  Starting the standard location service
- (void)startStandardUpdates
{
    // 创建location manager
    if (nil == locationManager)
        locationManager = [[CLLocationManager alloc] init];
 
    locationManager.delegate = self;

  // 设置获取位置的精确度,越精确越耗电
    locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
 
    // 设置距离过滤器,超过次距离就更新一次位置
    locationManager.distanceFilter = 500;
 
    [locationManager startUpdatingLocation];
}

Listing 2 Significant-Change Location Service

- (void)startSignificantChangeUpdates
{
    // Create the location manager if this object does not
    // already have one.
    if (nil == locationManager)
        locationManager = [[CLLocationManager alloc] init];
 
    locationManager.delegate = self;
    [locationManager startMonitoringSignificantLocationChanges];
}

4. 检查硬件

+ (BOOL)locationServicesEnabled; // has the user enabled location monitoring in Settings? 
+ (BOOL)headingAvailable; // can this hardware provide heading info (compass)? 
+ (BOOL)significantLocationChangeMonitoringAvailable; // only if device has cellular?
+ (BOOL)regionMonitoringAvailable; // only certain iOS4 devices
+ (BOOL)regionMonitoringEnabled; // by the user in Settings

当程序初次使用位置服务时,会询问用户。可以提供一个string来描述使用目的。如果用户拒绝,则上面所有方法均返回NO

 @property (copy) NSString *purpose


5. 获取位置更新

// Delegate method from the CLLocationManagerDelegate protocol.
- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
    fromLocation:(CLLocation *)oldLocation
{
    // If it's a relatively recent event, turn off updates to save power
    NSDate* eventDate = newLocation.timestamp;
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
    if (abs(howRecent) < 15.0)
    {
        NSLog(@"latitude %+.6f, longitude %+.6f\n",
                newLocation.coordinate.latitude,
                newLocation.coordinate.longitude);
    }
    // else skip the event and process the next one.
}

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error;

typedef enum {
   kCLErrorLocationUnknown  = 0,
   kCLErrorDenied,                           // 如果用户拒绝开启位置服务,那么应该停止location manager
   kCLErrorNetwork,
   kCLErrorHeadingFailure,
   kCLErrorRegionMonitoringDenied,
   kCLErrorRegionMonitoringFailure,
   kCLErrorRegionMonitoringSetupDelayed,
} CLError;


6. Geocoding Location Data

Geocoder对象使用网络服务来将经纬度转换为具体地址信息,iOS当前只支持经纬度转地址信息,不能将位置信息转换为经纬度

创建一个MKReverseGeocoder实例,设置代理,调用start方法。
代理会接受到 reverseGeocoder:didFindPlacemark:和reverseGeocoder:didFailWithError:


@implementation MyGeocoderViewController (CustomGeocodingAdditions)  
- (void)geocodeLocation:(CLLocation*)location forAnnotation:(MapLocation*)annotation  
{  
    MKReverseGeocoder* theGeocoder = [[MKReverseGeocoder alloc] initWithCoordinate:location.coordinate];  
   
    theGeocoder.delegate = self;  
    [theGeocoder start];  
}  
   
// Delegate methods  
- (void)reverseGeocoder:(MKReverseGeocoder*)geocoder didFindPlacemark:(MKPlacemark*)place  
{  
    MapLocation*    theAnnotation = [map annotationForCoordinate:place.coordinate];  
    if (!theAnnotation)  
        return;  
   
   // Associate the placemark with the annotation.  
    theAnnotation.placemark = place;  
   
    // Add a More Info button to the annotation's view.  
    MKPinAnnotationView*  view = (MKPinAnnotationView*)[map viewForAnnotation:annotation];  
    if (view && (view.rightCalloutAccessoryView == nil))  
    {  
        view.canShowCallout = YES;  
        view.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];  
    }  
}  
   
- (void)reverseGeocoder:(MKReverseGeocoder*)geocoder didFailWithError:(NSError*)error  
{  
    NSLog(@"Could not retrieve the specified place information.\n");  
}  
@end  
   
@implementation MKMapView (GeocoderAdditions)  
   
- (MapLocation*)annotationForCoordinate:(CLLocationCoordinate2D)coord  
{  
    // Iterate through the map view's list of coordinates  
    // and return the first one whose coordinate matches  
    // the specified value exactly.  
    id<MKAnnotation> theObj = nil;  
   
    for (id obj in [self annotations])  
    {  
        if (([obj isKindOfClass:[MapLocation class]]))  
        {  
            MapLocation* anObj = (MapLocation*)obj;  
   
            if ((anObj.coordinate.latitude == coord.latitude) &&  
                (anObj.coordinate.longitude == coord.longitude))  
            {  
                theObj = anObj;  
                break;  
            }  
        }  
    }  
  
    return theObj;  
}  
@end



MapKit

1. MKMapView 显示地图

2. 地图上可以显示注释(annotation),每个annotation由coordinate,title,subtitle构成,并由MKAnnotationView来显示

3. Annotation可以有一个callout,当annotation view被点击时显示,默认情况下只是显示title和subtitle,不过你可以添加左右accessory views

4. MKMapView显示一个遵守MKAnnotation协议的数组对象

@property (readonly) NSArray *annotations; // contains id <MKAnnotation> objects
MKAnnotation protocol
@protocol MKAnnotation <NSObject>
@property (readonly) CLLocationCoordinate2D coordinate;
@optional
@property (readonly) NSString *title;
@property (readonly) NSString *subtitle;
@end
typedef {
    CLLocationDegrees latitude;
    CLLocationDegrees longitude;
} CLLocationCoordinate2D;




5. 管理map view的annotations的方法

- (void)addAnnotation:(id <MKAnnotation>)annotation;
- (void)addAnnotations:(NSArray *)annotations;
- (void)removeAnnotation:(id <MKAnnotation>)annotation;
- (void)removeAnnotations:(NSArray *)annotations;

6. MKMapView使用跟TableView类似的方法来重用annotation view

7. Annotations通过MKAnnotationView的子类显示在地图上,默认为MKPinAnnotationView

8. 如果MKAnnotationView的canShowCallout设置为YES,那么点击会显示,同时会调用

- (void)mapView:(MKMapView *)sender didSelectAnnotationView:(MKAnnotationView *)aView;
//This is a great place to set up the MKAnnotationView‘s callout accessory views lazily.

9. MKAnnotationViews的创建方法跟tableviewcell在tableview中的创建类似

- (MKAnnotationView *)mapView:(MKMapView *)sender
            viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKAnnotationView *aView = [sender dequeueReusableAnnotationViewWithIdentifier:IDENT];
    if (!aView) {
        aView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
                                                reuseIdentifier:IDENT];
        // set canShowCallout to YES and build aView’s callout accessory views here 
             }
    aView.annotation = annotation; // yes, this happens twice if no dequeue
    // maybe load up accessory views here (if not too expensive)?
    // or reset them and wait until mapView:didSelectAnnotationView: to load actual data
    return aView;
}

10. MKAnnotationView的一些属性

@property id <MKAnnotation> annotation; // the annotation; treat as if readonly 
@property UIImage *image; // instead of the pin, for example
@property UIView *leftCalloutAccessoryView; // maybe a UIImageView
@property UIView *rightCalloutAccessoryView; // maybe a “disclosure” UIButton 
@property BOOL enabled; // NO means it ignores touch events, no delegate method, no callout 
@property CGPoint centerOffset; // where the “head of the pin” is relative to the image 
@property BOOL draggable; // only works if the annotation implements setCoordinate:

11. 如果你设置一个callout accessory view为一个UIControl

e.g. aView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
The following MKMapViewDelegate method will get called when the accessory view is touched ...

- (void)mapView:(MKMapView *)sender 
        annotationView:(MKAnnotationView *)aView
        calloutAccessoryControlTapped:(UIControl *)control;

12. 使用一下代理方法来加载accessory views

-(void)mapView:(MKMapView *)sender didSelectAnnotationView:(MKAnnotationView *)aView 
{
   if ([aView.leftCalloutAccessoryView isKindOfClass:[UIImageView class]]) {
          UIImageView *imageView = (UIImageView *)aView.leftCalloutAccessoryView; 
          imageView.image = ...; // if you do this in a GCD queue, be careful, views are reused!
   }
}

13. 地图类型

@property MKMapType mapType;
enum {  
    MKMapTypeStandard = 0,  
    MKMapTypeSatellite,  
    MKMapTypeHybrid  
};  
typedef NSUInteger MKMapType;  



14. 显示用户当前地址

@property BOOL showsUserLocation;
@property (readonly) BOOL isUserLocationVisible;
@property (readonly) MKUserLocation *userLocation;
MKUserLocation is an object which conforms to MKAnnotation which holds the user’s location.

15.限制用户对地图的操作

@property BOOL zoomEnabled;
@property BOOL scrollEnabled;

16. 控制地图的显示区域

@property MKCoordinateRegion region;
typedef struct {
    CLLocationCoordinate2D center;
    MKCoordinateSpan span;
} MKCoordinateRegion;

typedef struct {
    CLLocationDegrees latitudeDelta;
    CLLocationDegrees longitudeDelta;
}
- (void)setRegion:(MKCoordinateRegion)region animated:(BOOL)animated; // animate
注意:
你赋给region属性的值通常不是最终保存的值,在设置显示区域的时候同时也设置了缩放等级。map view不能显示任意的缩放等级,因此map view会选择一个能够尽可能显示你指定区域大小的缩放等级,然后根据此时显示的区域来保存。

@property CLLocationCoordinate2D centerCoordinate;
- (void)setCenterCoordinate:(CLLocationCoordinate2D)center animated:(BOOL)animated;

17. 地图载入通知

Remember that the maps are downloaded from Google earth.
- (void)mapViewWillStartLoadingMap:(MKMapView *)sender;
- (void)mapViewDidFinishLoadingMap:(MKMapView *)sender;
- (void)mapViewDidFailLoadingMap:(MKMapView *)sender withError:(NSError *)error;

18.








基于实时迭代的数值鲁棒NMPC双模稳定预测模型(Matlab代码实现)内容概要:本文介绍了基于实时迭代的数值鲁棒非线性模型预测控制(NMPC)双模稳定预测模型的研究与Matlab代码实现,重点在于通过数值方法提升NMPC在动态系统中的鲁棒性与稳定性。文中结合实时迭代机制,构建了能够应对系统不确定性与外部扰动的双模预测控制框架,并利用Matlab进行仿真验证,展示了该模型在复杂非线性系统控制中的有效性与实用性。同时,文档列举了大量相关的科研方向与技术应用案例,涵盖优化调度、路径规划、电力系统管理、信号处理等多个领域,体现了该方法的广泛适用性。; 适合人群:具备一定控制理论基础Matlab编程能力,从事自动化、电气工程、智能制造等领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①用于解决非线性动态系统的实时控制问题,如机器人控制、无人机路径跟踪、微电网能量管理等;②帮助科研人员复现论文算法,开展NMPC相关创新研究;③为复杂系统提供高精度、强鲁棒性的预测控制解决方案。; 阅读建议:建议读者结合提供的Matlab代码进行仿真实践,重点关注NMPC的实时迭代机制与双模稳定设计原理,并参考文档中列出的相关案例拓展应用场景,同时可借助网盘资源获取完整代码与数据支持。
UWB-IMU、UWB定位对比研究(Matlab代码实现)内容概要:本文介绍了名为《UWB-IMU、UWB定位对比研究(Matlab代码实现)》的技术文档,重点围绕超宽带(UWB)与惯性测量单元(IMU)融合定位技术展开,通过Matlab代码实现对两种定位方式的性能进行对比分析。文中详细阐述了UWB单独定位与UWB-IMU融合定位的原理、算法设计及仿真实现过程,利用多传感器数据融合策略提升定位精度与稳定性,尤其在复杂环境中减少信号遮挡漂移误差的影响。研究内容包括系统建模、数据预处理、滤波算法(如扩展卡尔曼滤波EKF)的应用以及定位结果的可视化与误差分析。; 适合人群:具备一定信号处理、导航定位或传感器融合基础知识的研究生、科研人员及从事物联网、无人驾驶、机器人等领域的工程技术人员。; 使用场景及目标:①用于高精度室内定位系统的设计与优化,如智能仓储、无人机导航、工业巡检等;②帮助理解多源传感器融合的基本原理与实现方法,掌握UWB与IMU互补优势的技术路径;③为相关科研项目或毕业设计提供可复现的Matlab代码参考与实验验证平台。; 阅读建议:建议读者结合Matlab代码逐段理解算法实现细节,重点关注数据融合策略与滤波算法部分,同时可通过修改参数或引入实际采集数据进行扩展实验,以加深对定位系统性能影响因素的理解。
本系统基于MATLAB平台开发,适用于2014a、2019b及2024b等多个软件版本,并提供了可直接执行的示例数据集。代码采用模块化设计,关键参数均可灵活调整,程序结构逻辑分明且附有详细说明注释。主要面向计算机科学、电子信息工程、数学等相关专业的高校学生,适用于课程实验、综合作业及学位论文等教学与科研场景。 水声通信是一种借助水下声波实现信息传输的技术。近年来,多输入多输出(MIMO)结构与正交频分复用(OFDM)机制被逐步整合到水声通信体系中,显著增强了水下信息传输的容量与稳健性。MIMO配置通过多天线收发实现空间维度上的信号复用,从而提升频谱使用效率;OFDM方案则能够有效克服水下信道中的频率选择性衰减问题,保障信号在复杂传播环境中的可靠送达。 本系统以MATLAB为仿真环境,该工具在工程计算、信号分析与通信模拟等领域具备广泛的应用基础。用户可根据自身安装的MATLAB版本选择相应程序文件。随附的案例数据便于快速验证系统功能与性能表现。代码设计注重可读性与可修改性,采用参数驱动方式,重要变量均设有明确注释,便于理解与后续调整。因此,该系统特别适合高等院校相关专业学生用于课程实践、专题研究或毕业设计等学术训练环节。 借助该仿真平台,学习者可深入探究水声通信的基础理论及其关键技术,具体掌握MIMO与OFDM技术在水声环境中的协同工作机制。同时,系统具备良好的交互界面与可扩展架构,用户可在现有框架基础上进行功能拓展或算法改进,以适应更复杂的科研课题或工程应用需求。整体而言,该系统为一套功能完整、操作友好、适应面广的水声通信教学与科研辅助工具。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
数据结构部分 -- 一、栈队列 Stack && Queue 栈 - 结构图 alt 队列 - 结构图 alt 双端队列 - 结构图 alt 二、 链表 Linked List 单链表 - 结构图 alt 单项循环链表 - 结构图 alt 双向链表 - 结构图 alt 三、 树 基础定义及相关性质内容 - 结构图 alt - 另外可以参考浙江大学数据结构课程中关于遍历方式的图,讲的十分详细 alt 使用链表实现二叉树 二叉查找树 - 非空左子树的所有键值小于根节点的键值 - 非空右子树的所有键值大于根节点的键值 - 左右子树都是二叉查找树 补充 - 完全二叉树 - 如果二叉树中除去最后一层节点为满二叉树,且最后一层的结点依次从左到右分布,则此二叉树被称为完全二叉树。 - 满二叉树 - 如果二叉树中除了叶子结点,每个结点的度都为 2,则此二叉树称为满二叉树。 代码下载地址: https://pan.quark.cn/s/b48377ea3e78 四、 堆 Heap 堆满足的条件 - 必须是完全二叉树 - 各个父节点必须大于或者小于左右节点,其中最顶层的根结点必须是最大或者最小的 实现方式及条件 - 使用数组实现二叉堆,例如下图的最大堆,在数组中使用[0,100,90,85,80,30,60,50,55]存储,注意上述第一个元素0仅仅是做占位; - 设节点位置为x,则左节点位置为2x,右节点在2x+1;已知叶子节点x,根节点为x//2; - 举例说明: - 100为根节点(位置为1),则左节点位置为2,即90,右节点位置为3,即85; - 30为子节点(位置为5),则根节点为(5//2=2),即90; 根据上述条件,我们可以绘制出堆的两种形式 - 最大堆及实现 al...
基于自抗扰控制ADRC的永磁同步电机仿真模型(Simulink仿真实现)内容概要:本文介绍了基于自抗扰控制(ADRC)的永磁同步电机(PMSM)仿真模型,利用Simulink平台实现控制系统的设计与仿真。该模型重点突出ADRC在抑制外部干扰系统参数不确定性方面的优势,通过构建PMSM的数学模型,结合ADRC控制器设计,有效提升了电机在复杂工况下的速度控制精度与动态响应性能。文中详细阐述了ADRC的核心结构,包括跟踪微分器(TD)、扩张状态观测器(ESO)非线性状态误差反馈控制律(NLSEF),并通过仿真验证了其相较于传统PID控制在抗干扰能力鲁棒性方面的优越性。; 适合人群:具备自动控制理论基础、电机控制相关知识以及Simulink仿真经验的高校学生、科研人员及工程技术人员;尤其适合从事电机驱动、高性能伺服系统或先进控制算法研究的专业人士。; 使用场景及目标:① 掌握自抗扰控制的基本原理及其在电机控制中的具体应用;② 学习如何在Simulink中搭建永磁同步电机控制系统并实现ADRC算法;③ 对比分析ADRC与传统控制方法在抗扰动、鲁棒性动态性能方面的差异;④ 为实际工程中高性能电机控制系统的开发提供仿真验证基础技术参考。; 阅读建议:建议读者结合控制理论基础知识,逐步理解ADRC各模块的设计思想,并动手在Simulink中复现仿真模型,通过调整参数观察系统响应变化,深入掌握ADRC的调节规律与优化方法。同时可扩展研究不同工况下的控制效果,进一步提升系统性能。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值