Core Location和MapKit的一些简单使用
Core Location
1. 基本对象是CLLocation,有属性coordinate, altitude, horizontal/vertical Accuracy, timestamp, speed, course
- <span style="font-size:18px;">typedef {
- CLLocationDegrees latitude; // a double
- CLLocationDegrees longitude; // a double
- } CLLocationCoordinate2D; // 经纬度</span>
- @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
- <pre name="code" class="plain">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;</pre><br>
- <br>
- <pre></pre>
- <p></p>
- <pre></pre>
- <p></p>
- <p><span style="font-size:18px">5. 管理map view的annotations的方法</span></p>
- <p><span style="font-size:18px"></span></p>
- <pre name="code" class="plain">- (void)addAnnotation:(id <MKAnnotation>)annotation;
- - (void)addAnnotations:(NSArray *)annotations;
- - (void)removeAnnotation:(id <MKAnnotation>)annotation;
- - (void)removeAnnotations:(NSArray *)annotations;</pre><br>
- 6. MKMapView使用跟TableView类似的方法来重用annotation view
- <p></p>
- <p><span style="font-size:18px">7. Annotations通过MKAnnotationView的子类显示在地图上,默认为MKPinAnnotationView<br>
- </span></p>
- <p><span style="font-size:18px">8. 如果MKAnnotationView的canShowCallout设置为YES,那么点击会显示,同时会调用</span></p>
- <p><span style="font-size:18px"></span></p>
- <pre name="code" class="plain">- (void)mapView:(MKMapView *)sender didSelectAnnotationView:(MKAnnotationView *)aView;
- //This is a great place to set up the MKAnnotationView‘s callout accessory views lazily.
- </pre>
- <p></p>
- <p><span style="font-size:18px">9. MKAnnotationViews的创建方法跟tableviewcell在tableview中的创建类似</span></p>
- <p><span style="font-size:18px"></span></p>
- <pre name="code" class="plain">- (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)?
- <span style="color:#FF0000;"> // or reset them and wait until mapView:didSelectAnnotationView: to load actual data</span>
- return aView;
- }
- </pre>
- <p></p>
- <p><span style="font-size:18px">10. MKAnnotationView的一些属性</span></p>
- <p><span style="font-size:18px"></span></p>
- <pre name="code" class="plain">@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:</pre><br>
- <span style="font-size:18px">11. 如果你设置一个callout accessory view为一个UIControl</span>
- <p></p>
- <p><span style="font-size:18px"></span></p>
- <pre name="code" class="plain">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;</pre><br>
- <span style="font-size:18px">12. 使用一下代理方法来加载accessory views</span>
- <p></p>
- <p><span style="font-size:18px"></span></p>
- <pre name="code" class="plain">-(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!
- }
- }</pre>
- <p></p>
- <p><span style="font-size:18px">13. 地图类型</span></p>
- <p><span style="font-size:18px"></span></p>
- <pre name="code" class="plain">@property MKMapType mapType;
- <pre name="code" class="plain">enum {
- MKMapTypeStandard = 0,
- MKMapTypeSatellite,
- MKMapTypeHybrid
- };
- typedef NSUInteger MKMapType; </pre><br>
- <pre></pre>
- <p></p>
- <pre></pre>
- <p></p>
- <p><span style="font-size:18px">14. 显示用户当前地址</span></p>
- <p><span style="font-size:18px"></span></p>
- <pre name="code" class="plain">@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.</pre><br>
- <span style="font-size:18px">15.限制用户对地图的操作</span>
- <p></p>
- <p><span style="font-size:18px"></span></p>
- <pre name="code" class="plain">@property BOOL zoomEnabled;
- @property BOOL scrollEnabled;</pre><br>
- <span style="font-size:18px">16. 控制地图的显示区域</span>
- <p></p>
- <p><span style="font-size:18px"></span></p>
- <pre name="code" class="plain">@property MKCoordinateRegion region;
- typedef struct {
- CLLocationCoordinate2D center;
- MKCoordinateSpan span;
- } MKCoordinateRegion;
- typedef struct {
- CLLocationDegrees latitudeDelta;
- CLLocationDegrees longitudeDelta;
- }
- - (void)setRegion:(MKCoordinateRegion)region animated:(BOOL)animated; // animate</pre><span style="font-size:18px">注意:</span><br>
- <span style="font-size:18px">你赋给region属性的值通常不是最终保存的值,在设置显示区域的时候同时也设置了缩放等级。map view不能显示任意的缩放等级,因此map view会选择一个能够尽可能显示你指定区域大小的缩放等级,然后根据此时显示的区域来保存。</span><br>
- <br>
- <pre name="code" class="plain">@property CLLocationCoordinate2D centerCoordinate;
- - (void)setCenterCoordinate:(CLLocationCoordinate2D)center animated:(BOOL)animated;</pre><br>
- <span style="font-size:18px">17. 地图载入通知</span>
- <p></p>
- <p><span style="font-size:18px"></span></p>
- <pre name="code" class="plain">Remember that the maps are downloaded from Google earth.
- - (void)mapViewWillStartLoadingMap:(MKMapView *)sender;
- - (void)mapViewDidFinishLoadingMap:(MKMapView *)sender;
- - (void)mapViewDidFailLoadingMap:(MKMapView *)sender withError:(NSError *)error;</pre>
- <p></p>
- <p><span style="font-size:18px">18. <br>
- </span></p>
- <p><span style="font-size:18px"><br>
- </span></p>
- <p><span style="font-size:18px"><br>
- </span></p>
- <p><span style="font-size:18px"><br>
- </span></p>
- <p><span style="font-size:18px"><br>
- </span></p>
- <p><span style="font-size:18px"><br>
- </span></p>
- <p><span style="font-size:18px"><br>
- </span></p>
- <p><br>
- </p>
- </pre>
Core Location和MapKit的一些简单使用的更多相关文章
- iOS 苹果自带地图定位Core Location
Core Location是iOS SDK中一个提供设备位置的框架.可以使用三种技术来获取位置:GPS.蜂窝或WiFi.在这些技术中,GPS最为精准,如果有GPS硬件,Core Location将优先 ...
- iPhone的定位技术与Core Location框架
来源:http://www.cnblogs.com/lovecode/archive/2011/12/24/2300579.html iPhone定位来源通常有:1. GPS定位 2. WiFi定位 ...
- iOS开发-Core Location和Map Kit
一.Core Location确定物理位置 利用以下3种技术: 1.GPS(最精确的) 2.蜂窝基站ID定位(cell ID Location) 3.WPS(Wi-Fi Positioning Ser ...
- 关于Core Location-ios定位
IOS中的core location提供了定位功能,能定位装置的当前坐标,同一时候能得到装置移动信息.由于对定位装置的轮询是非常耗电的,所以最好仅仅在非常必要的前提下启动. 当中,最重要的类是CLLo ...
- IOS开发之Core Location
IOS 支持三种检测当前位置的方式:手机基站.Wi-Fi.和GPS,其中GPS是经度最高的,同时也是最耗费手机电量的.一般情况下在室内是无法通过GPS获 取位置信息的,通过Wi-Fi获取位置的原理是通 ...
- ios中Core Location跟Map Kit的基本使用
地图类开发应用中,离不开地理位置跟MKMapView的使用,下面就记录下自己在使用这两个东西中学到的. 不过并不是所有苹果的设备都支持地理位置,我们在使用前应该做个判断,代码如下: BOOL loca ...
- Core Location :⽤用于地理定位
Core Location :⽤用于地理定位 在移动互联⽹网时代,移动app能解决⽤用户的很多⽣生活琐事,⽐比如 导航:去任意陌⽣生的地⽅方 周边:找餐馆.找酒店.找银⾏行.找电影院 在上述应⽤用中, ...
- 并发编程概述 委托(delegate) 事件(event) .net core 2.0 event bus 一个简单的基于内存事件总线实现 .net core 基于NPOI 的excel导出类,支持自定义导出哪些字段 基于Ace Admin 的菜单栏实现 第五节:SignalR大杂烩(与MVC融合、全局的几个配置、跨域的应用、C/S程序充当Client和Server)
并发编程概述 前言 说实话,在我软件开发的头两年几乎不考虑并发编程,请求与响应把业务逻辑尽快完成一个星期的任务能两天完成绝不拖三天(剩下时间各种浪),根本不会考虑性能问题(能接受范围内).但随着工 ...
- Core Location Framework学习
在Apple开发中,尤其是移动设备开发,经常会使用Core Location Framework,这个框架可以使得iOS设备获取当前的地理位置.本文就具体到Core Location 框架中,查看其声 ...
随机推荐
- iosxcode7以后免证书真机测试方法如下
步骤比较简单,我就简单总结一下. 1. 进入xcode,菜单栏选择xcode –> preferences (快捷键 command + ,)在Accounts选项卡添加自己的Apple ID ...
- phalcon的url大小写的问题
一开始我以为url的大小写是不区分的,实际上调试时是可以发现获取到的url是大小写是和请求时一致, 所谓的没区分,只是服务器或者相应的代码做的处理. 在phalcon里如果路由是api/test,则会 ...
- C#创建资源文件
资源文件顾名思义就是存放资源的文件.资源文件在程序设计中有着自身独特的优势,他独立于源程序,这样资源文件就可以被多个程序使用.同时在程序设计的时候,有时出于安全或者其他方面因素的考虑,把重要东西存放在 ...
- C语言 · 求圆面积表面积体积
算法提高 3-3求圆面积表面积体积 时间限制:1.0s 内存限制:256.0MB 问题描述 接受用户输⼊的数值,输出以该值为半径的(1)圆面积,(2)球体表面积,(3)球体体积.pi ...
- jQuery EasyUI教程之datagrid应用-2
二.DataGrid的扩展应用 上一份教程我们创建的一个CRUD应用是使用对话框组件来增加或编辑用户信息.本教程将教你如何创建一个CRUD 数据表格(datagrid). 为了让这些CRUD功能正常工 ...
- Windoows窗口程序五
程序执行机制 过程驱动-程序的执行过程是按照预订好的顺序执行. 事件驱动-程序的执行是无序,用户可以根据需要随机触发相应的事件. Win32窗口程序就是采用事件驱动方式执行,也就是消息机制. 当系统通 ...
- thinkphp3.2 常用入口文件
<?php define('DIR_SECURE_FILENAME', 'default.html'); define('APP_PATH','./index/'); //项目路径 requir ...
- 关于Cocos Creator用js脚本代码播放骨骼动画的步骤和注意事项
步骤: 1.用cc.find()方法找到相应的骨骼动画节点,并把这个对象赋值给一个var出来的新对象. 具体代码:var spineboy_anim = cc.find("UI_Root/a ...
- C# HttpClientHelper请求
public class HttpClientHelper { /// <summary> /// get请求 /// </summary> /// <param nam ...
- mysql 从sql存储文件恢复数据库乱码
场景一: 一台电脑上导出的sql文件到另一台电脑上恢复数据库,汉字全部是乱码,然后可能还有部分数据提示超长. 场景二: 拿到的sql文件不是原始的导出sql文件,只有表结构和表数据,出现的问题和场景一 ...