ios之Core Location定位功能
来源:互联网 发布:union sql 编辑:程序博客网 时间:2024/05/14 07:01
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
- ios之Core Location定位功能
- IOS Core Location 定位功能
- iOS Location 定位功能
- ios-使用Core Location定位
- iOS Core Location 实现定位
- IOS学习之Core Location定位和MapKit视图使用
- IOS Core Location-ios定位
- 关于Core Location-ios定位
- 关于Core Location-ios定位
- 关于Core Location-ios定位
- 关于Core Location-ios定位
- ios-使用Core Location框架定位
- iOS定位服务Core Location使用详解
- 关于Core Location-ios定位
- IOS开发之Core Location
- IOS开发之Core Location
- iOS开发之core location
- iOS开发之-Core Location
- oracle 优化规则(3)
- IOS 字符串操作
- oracle 优化规则(4)
- 简单的xml读取
- oracle 优化规则(5)
- ios之Core Location定位功能
- 1.多态
- 在oracle中用sql脚本生成csv文件举例
- 用wact 进行iis 压力测试
- 存储过程的基本应用
- 正则表达式分类 区别
- oracle创建触发器调用含参数存储过程
- struct和typedef struct
- Firefox浏览IIS本地 需要用户名密码的解决方案