iphone开发之GPS定位

#import "RootViewController.h"

@interface RootViewController ()
@end

@implementation RootViewController

@synthesize locationManager;
@synthesize startingPoint;
@synthesize latitudeLabel;
@synthesize longitudeLabel;
@synthesize horizontalAccuracyLabel;
@synthesize altitudeLabel;
@synthesize verticalAccuracyLabel;
@synthesize distanceTraveledLabel;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor blackColor];
    
    self.latitudeLabel=[[UILabel alloc]initWithFrame:CGRectMake(60, 30, 200, 30)];
    self.latitudeLabel.backgroundColor=[UIColor whiteColor];
    
    self.longitudeLabel=[[UILabel alloc]initWithFrame:CGRectMake(60, 70, 200, 30)];
    self.longitudeLabel.backgroundColor=[UIColor whiteColor];

    self.horizontalAccuracyLabel=[[UILabel alloc]initWithFrame:CGRectMake(60, 110, 200, 30)];
    self.horizontalAccuracyLabel.backgroundColor=[UIColor whiteColor];

    self.altitudeLabel=[[UILabel alloc]initWithFrame:CGRectMake(60, 150, 200, 30)];
    self.altitudeLabel.backgroundColor=[UIColor whiteColor];

    self.verticalAccuracyLabel=[[UILabel alloc]initWithFrame:CGRectMake(60, 190, 200, 30)];
    self.verticalAccuracyLabel.backgroundColor=[UIColor whiteColor];

    self.distanceTraveledLabel=[[UILabel alloc]initWithFrame:CGRectMake(60, 230, 200, 30)];
    self.distanceTraveledLabel.backgroundColor=[UIColor whiteColor];


    [self.view addSubview:self.latitudeLabel];
    [self.view addSubview:self.longitudeLabel];
    [self.view addSubview:self.horizontalAccuracyLabel];
    [self.view addSubview:self.altitudeLabel];
    [self.view addSubview:self.verticalAccuracyLabel];
    [self.view addSubview:self.distanceTraveledLabel];
    
    self.locationManager=[[CLLocationManager alloc]init];
    locationManager.delegate=self;
    locationManager.desiredAccuracy=kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    if (startingPoint==nil) {
        self.startingPoint=[locations objectAtIndex:[locations count]-1];
        NSString *latitude=[NSString stringWithFormat:@"%g\u00B0",self.startingPoint.coordinate.latitude];
        latitudeLabel.text=latitude;
        NSString *longitude=[NSString stringWithFormat:@"%g\u00B0",self.startingPoint.coordinate.longitude];
        longitudeLabel.text=longitude;
        NSString *horizontal=[NSString stringWithFormat:@"%gm",self.startingPoint.horizontalAccuracy];
        horizontalAccuracyLabel.text=horizontal;
        NSString *altitude=[NSString stringWithFormat:@"%gm",self.startingPoint.altitude];
        altitudeLabel.text=altitude;
        NSString *vertical=[NSString stringWithFormat:@"%gm",self.startingPoint.verticalAccuracy];
        verticalAccuracyLabel.text=vertical;
        CLLocationDirection distance=[self.startingPoint distanceFromLocation:startingPoint];
        NSString *distances=[NSString stringWithFormat:@"%gm",distance];
        distanceTraveledLabel.text=distances;
    }
}


-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    NSString *errorType=(error.code==kCLErrorDenied)?@"Access Denide":@"Unknown Error";
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Error getting Location" message:errorType delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
    [alert show];
}

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值