cocoa touch layer下面的几个点连载之--CoreLocation

cocoa touch layer下面的几个点连载之--CoreLocation,第1张

概述Core Location框架提供了三种用于追踪设备当前位置的服务,Core Location框架从内置的蜂窝,Wi-Fi或者GPS来获取位置The significant-change locationservice 提供了低耗电的方法来获取当前位置,当前位置改变时会发出通知The standard location service 提供了一种可设置的方法来获取当前位置Region mo
Core Location框架提供了三种用于追踪设备当前位置的服务,Core Location框架从内置的蜂窝,Wi-Fi或者GPS来获取位置The significant-change locationservice 提供了低耗电的方法来获取当前位置,当前位置改变时会发出通知The standard location service 提供了一种可设置的方法来获取当前位置Region monitoring 监视特定地区的跨越 如果程序必须使用位置服务在程序的info.pList中添加UI@R_502_4173@DeviceCapabilitIEs键,它是一个包含多个字符串的数组,然后添加location-services,gps字符串1.The Standard Location Service <span >Listing 1-1  Starting the standard location service- (voID)startStandardUpdates{    // 创建location manager    if (nil == locationManager)        locationManager = [[CLLocationManager alloc] init];     locationManager.delegate = self;</span>复制代码<span >  // 设置获取位置的精确度,越精确越耗电</span>复制代码<span >    locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;     // 设置距离过滤器,超过次距离就更新一次位置    locationManager.distanceFilter = 500;     [locationManager startUpdatingLocation];}</span>复制代码使用location manager之前一般要检查位置服务是否可用,<span >+ (BOol)locationServicesEnabled</span>复制代码当位置信息更新时,会给location manager发送消息2.Significant-Change Location Service<span >- (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];}</span>复制代码可以叫醒在后台的程序3.Region monitoring Service使用之前调用CLLocationManager的regionMonitoringAvailable and regionMonitoringEnabled <span >- (BOol)registerRegionWithCircularOverlay:(MyCircle*)overlay andIDentifIEr:(Nsstring*)IDentifIEr{   // Do not create regions if support is unavailable or Disabled.   if ( ![CLLocationManager regionMonitoringAvailable] ||        ![CLLocationManager regionMonitoringEnabled] )      return NO;    // If the radius is too large,registration fails automatically,// so clamp the radius to the max value.   CLLocationdegrees radius = overlay.radius;   if (radius > self.locationManager.maximumRegionMonitoringdistance)      radius = self.locationManager.maximumRegionMonitoringdistance;    // Create the region and start monitoring it.   CLRegion* region = [[CLRegion alloc] initCircularRegionWithCenter:overlay.coordinate                        radius:radius IDentifIEr:IDentifIEr];   [self.locationManager startMonitoringForRegion:region                    desiredAccuracy:kCLLocationAccuracyHundredMeters];    [region release];   return YES;}</span>复制代码接受位置更新消息当位置更新时,location manager向其代理发送 locationManager:dIDUpdatetoLocation:fromLocation:如果无法获取新位置,则发送locationManager:dIDFailWithError:<span >// 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.}</span>复制代码位置更新错误 locationManager:dIDFailWithError:<span >    if ([error code] != kCLErrorLocationUnkNown) {        [self stopUpdatingLocation:NSLocalizedString(@"Error",@"Error")];</span>复制代码If the location service is unable to retrIEve a location fix right away,it reports a kCLErrorLocationUnkNownerror and keeps trying. In such a situation,you can simply ignore the error and wait for a new event.If the user denIEs your application’s use of the location service,this method reports a kCLErrorDenIEd error. Upon receiving such an error,you should stop the location service.If a heading Could not be determined because of strong interference from nearby magnetic fIElds,this method returns kCLErrorheadingFailure.<span >typedef enum {   kCLErrorLocationUnkNown  = 0,kCLErrorDenIEd,kCLErrorNetwork,kCLErrorheadingFailure,kCLErrorRegionMonitoringDenIEd,kCLErrorRegionMonitoringFailure,kCLErrorRegionMonitoringSetupDelayed,} CLError;</span>复制代码获取移动方向信息<pre name="code" ><span >- (voID)startheadingEvents {    if (!self.locationManager) {       CLLocationManager* theManager = [[[CLLocationManager alloc] init] autorelease];         // Retain the object in a property.       self.locationManager = theManager;       self.locationManager.delegate = self;    }      // Start location services to get the true heading.    locationManager.distanceFilter = 1000;    locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;    [locManager startUpdatingLocation];      // 开始更新移动方向    if ([CLLocationManager headingAvailable]) {       locManager.headingFilter = 5;       [locManager startUpdatingheading];    } }  </span>复制代码<span >- (voID)locationManager:(CLLocationManager *)manager dIDUpdateheading:(CLheading *)newheading {    if (newheading.headingAccuracy < 0)       return;      // Use the true heading if it is valID.    CLLocationDirection  theheading = ((newheading.trueheading > 0) ?             newheading.trueheading : newheading.magneticheading);      self.currentheading = theheading;    [self updateheadingdisplays]; }  </span>复制代码GeoCoding Location DataGeocoder对象使用网络服务来将经纬度转换为具体地址信息,iOS当前只支持经纬度转地址信息,不能将位置信息转换为经纬度创建一个MKReverseGeocoder实例,设置代理,调用start方法。代理会接受到 reverseGeocoder:dIDFindplacemark:和reverseGeocoder:dIDFailWithError:<span >@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</span>
总结

以上是内存溢出为你收集整理的cocoa touch layer下面的几个点连载之--CoreLocation全部内容,希望文章能够帮你解决cocoa touch layer下面的几个点连载之--CoreLocation所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1059170.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存