ios之Core Location定位功能
2013-02-26 12:59
337 查看
1、需要遵循CLLocationManagerDelegate协议,该协议有两个方法
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation;//当位置管理器应经确定当前位置或者当他检测到未知的更改时调用该方法
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error ;//当位置管理器遇到错误时调用该方法
2、Core Location定位功能的步骤
1)、创建位置管理器
2)、设置委托
3)、设置需要的精度
4)、设置距离筛选器 //可选, 修改distanceFilter
5)、启动位置管理器
、h文件
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface WhereAmIViewController :
UIViewController <CLLocationManagerDelegate> {
CLLocationManager *locationManager;
CLLocation *startingPoint;
UILabel *latitudeLabel;
UILabel *longitudeLabel;
UILabel *horizontalAccuracyLabel;
UILabel *altitudeLabel;
UILabel *verticalAccuracyLabel;
UILabel *distanceTraveledLabel;
}
@property (retain, nonatomic) CLLocationManager *locationManager;
@property (retain, nonatomic) CLLocation *startingPoint;
@property (retain, nonatomic) IBOutlet UILabel *latitudeLabel;
@property (retain, nonatomic) IBOutlet UILabel *longitudeLabel;
@property (retain, nonatomic) IBOutlet UILabel *horizontalAccuracyLabel;
@property (retain, nonatomic) IBOutlet UILabel *altitudeLabel;
@property (retain, nonatomic) IBOutlet UILabel *verticalAccuracyLabel;
@property (retain, nonatomic) IBOutlet UILabel *distanceTraveledLabel;
@end
、m文件
#import "WhereAmIViewController.h"
@implementation WhereAmIViewController
@synthesize locationManager;
@synthesize startingPoint;
@synthesize latitudeLabel;
@synthesize longitudeLabel;
@synthesize horizontalAccuracyLabel;
@synthesize altitudeLabel;
@synthesize verticalAccuracyLabel;
@synthesize distanceTraveledLabel;
#pragma mark -
- (void)viewDidLoad {
self.locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.locationManager = nil;
self.latitudeLabel = nil;
self.longitudeLabel = nil;
self.horizontalAccuracyLabel = nil;
self.altitudeLabel = nil;
self.verticalAccuracyLabel = nil;
self.distanceTraveledLabel = nil;
[super viewDidUnload];
}
- (void)dealloc {
[locationManager release];
[startingPoint release];
[latitudeLabel release];
[longitudeLabel release];
[horizontalAccuracyLabel release];
[altitudeLabel release];
[verticalAccuracyLabel release];
[distanceTraveledLabel release];
[super dealloc];
}
#pragma mark -
#pragma mark CLLocationManagerDelegate Methods
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
if (startingPoint == nil)
self.startingPoint = newLocation;
NSString *latitudeString = [[NSString alloc] initWithFormat:@"%g°",
newLocation.coordinate.latitude];
latitudeLabel.text = latitudeString;
[latitudeString release];
NSString *longitudeString = [[NSString alloc] initWithFormat:@"%g°",
newLocation.coordinate.longitude];
longitudeLabel.text = longitudeString;
[longitudeString release];
NSString *horizontalAccuracyString = [[NSString alloc]
initWithFormat:@"%gm",
newLocation.horizontalAccuracy];
horizontalAccuracyLabel.text = horizontalAccuracyString;
[horizontalAccuracyString release];
NSString *altitudeString = [[NSString alloc] initWithFormat:@"%gm",
newLocation.altitude];
altitudeLabel.text = altitudeString;
[altitudeString release];
NSString *verticalAccuracyString = [[NSString alloc]
initWithFormat:@"%gm",
newLocation.verticalAccuracy];
verticalAccuracyLabel.text = verticalAccuracyString;
[verticalAccuracyString release];
CLLocationDistance distance = [newLocation
getDistanceFrom:startingPoint];
NSString *distanceString = [[NSString alloc]
initWithFormat:@"%gm", distance];
distanceTraveledLabel.text = distanceString;
[distanceString release];
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error {
NSString *errorType = (error.code == kCLErrorDenied) ?
@"Access Denied" : @"Unknown Error";
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Error getting Location"
message:errorType
delegate:nil
cancelButtonTitle:@"Okay"
otherButtonTitles:nil];
[alert show];
[alert release];
}
@end
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation;//当位置管理器应经确定当前位置或者当他检测到未知的更改时调用该方法
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error ;//当位置管理器遇到错误时调用该方法
2、Core Location定位功能的步骤
1)、创建位置管理器
2)、设置委托
3)、设置需要的精度
4)、设置距离筛选器 //可选, 修改distanceFilter
5)、启动位置管理器
、h文件
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface WhereAmIViewController :
UIViewController <CLLocationManagerDelegate> {
CLLocationManager *locationManager;
CLLocation *startingPoint;
UILabel *latitudeLabel;
UILabel *longitudeLabel;
UILabel *horizontalAccuracyLabel;
UILabel *altitudeLabel;
UILabel *verticalAccuracyLabel;
UILabel *distanceTraveledLabel;
}
@property (retain, nonatomic) CLLocationManager *locationManager;
@property (retain, nonatomic) CLLocation *startingPoint;
@property (retain, nonatomic) IBOutlet UILabel *latitudeLabel;
@property (retain, nonatomic) IBOutlet UILabel *longitudeLabel;
@property (retain, nonatomic) IBOutlet UILabel *horizontalAccuracyLabel;
@property (retain, nonatomic) IBOutlet UILabel *altitudeLabel;
@property (retain, nonatomic) IBOutlet UILabel *verticalAccuracyLabel;
@property (retain, nonatomic) IBOutlet UILabel *distanceTraveledLabel;
@end
、m文件
#import "WhereAmIViewController.h"
@implementation WhereAmIViewController
@synthesize locationManager;
@synthesize startingPoint;
@synthesize latitudeLabel;
@synthesize longitudeLabel;
@synthesize horizontalAccuracyLabel;
@synthesize altitudeLabel;
@synthesize verticalAccuracyLabel;
@synthesize distanceTraveledLabel;
#pragma mark -
- (void)viewDidLoad {
self.locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.locationManager = nil;
self.latitudeLabel = nil;
self.longitudeLabel = nil;
self.horizontalAccuracyLabel = nil;
self.altitudeLabel = nil;
self.verticalAccuracyLabel = nil;
self.distanceTraveledLabel = nil;
[super viewDidUnload];
}
- (void)dealloc {
[locationManager release];
[startingPoint release];
[latitudeLabel release];
[longitudeLabel release];
[horizontalAccuracyLabel release];
[altitudeLabel release];
[verticalAccuracyLabel release];
[distanceTraveledLabel release];
[super dealloc];
}
#pragma mark -
#pragma mark CLLocationManagerDelegate Methods
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
if (startingPoint == nil)
self.startingPoint = newLocation;
NSString *latitudeString = [[NSString alloc] initWithFormat:@"%g°",
newLocation.coordinate.latitude];
latitudeLabel.text = latitudeString;
[latitudeString release];
NSString *longitudeString = [[NSString alloc] initWithFormat:@"%g°",
newLocation.coordinate.longitude];
longitudeLabel.text = longitudeString;
[longitudeString release];
NSString *horizontalAccuracyString = [[NSString alloc]
initWithFormat:@"%gm",
newLocation.horizontalAccuracy];
horizontalAccuracyLabel.text = horizontalAccuracyString;
[horizontalAccuracyString release];
NSString *altitudeString = [[NSString alloc] initWithFormat:@"%gm",
newLocation.altitude];
altitudeLabel.text = altitudeString;
[altitudeString release];
NSString *verticalAccuracyString = [[NSString alloc]
initWithFormat:@"%gm",
newLocation.verticalAccuracy];
verticalAccuracyLabel.text = verticalAccuracyString;
[verticalAccuracyString release];
CLLocationDistance distance = [newLocation
getDistanceFrom:startingPoint];
NSString *distanceString = [[NSString alloc]
initWithFormat:@"%gm", distance];
distanceTraveledLabel.text = distanceString;
[distanceString release];
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error {
NSString *errorType = (error.code == kCLErrorDenied) ?
@"Access Denied" : @"Unknown Error";
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Error getting Location"
message:errorType
delegate:nil
cancelButtonTitle:@"Okay"
otherButtonTitles:nil];
[alert show];
[alert release];
}
@end
相关文章推荐
- 【iOS】7.4 定位服务->2.1.3.3 定位 - 官方框架CoreLocation 功能3:区域监听
- iOS 自带定位功能CoreLocation
- 【iOS】7.4 定位服务->2.1.3.2 定位 - 官方框架CoreLocation 功能2:地理编码和反地理编码
- 【iOS】7.4 定位服务->2.1.3.3 定位 - 官方框架CoreLocation 功能3:区域监听
- IOS Core Location 定位功能
- 【iOS】7.4 定位服务->2.1.3.1 定位 - 官方框架CoreLocation 功能1:地理定位
- IOS 定位 CoreLocation的封装
- iOS CoreLocation定位 使用方法
- iOS定位-利用CoreLocation.framework获取当前城市
- 关于Core Location-ios定位
- 【iOS】7.4 定位服务->2.1.2 定位 - 官方框架CoreLocation: CLLocationManager(位置管理器)
- iOS定位服务CoreLocation
- IOS Core Location-ios定位
- iOS中的定位-CoreLocation框架
- iPhone开发之CoreLocation定位功能(6)
- iOS定位开发之CoreLocation
- iOS开发 - CoreLocation地理定位
- 【iOS】7.4 定位服务->2.1.1 定位 - 官方框架CoreLocation: 请求用户授权
- IOS学习之Core Location定位和MapKit视图使用
- iOS中CoreLocation定位的代理方法不执行的解决办法。