iPhone开发之CoreLocation定位功能(6)

        学了iPhone的CoreLocation之后,再回想Android的定位开发,真是省事了不少,iPhone对定位功能开发这一模块封装的很好,只需几步,便可以获取到设备所在的位置等多项参数!

        1.启动XCode4.3.2,单击菜单项File->New->Project...,以Sigle View Application模板新建项目,并命名为WhereAmI:

       

        2.单击ViewControler.h头文件,因为CoreLocation框架并不属于UIKit框架,所以需要另外引入,并添加协议:

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface ViewController : UIViewController<CLLocationManagerDelegate>
{
    CLLocationManager *locationManager;
    CLLocation *startPoint;
    UILabel *latLabel;
    UILabel *lonLabel;
    UILabel *distance;
}

@property(retain,nonatomic)CLLocationManager *locationManager;
@property(retain,nonatomic)CLLocation *startPoint;

@property(retain,nonatomic)IBOutlet UILabel *latLabel;
@property(retain,nonatomic)IBOutlet UILabel *lonLabel;
@property(retain,nonatomic)IBOutlet UILabel *distance;

@end


       3.单击ViewControler.m文件,添加以下代码:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize startPoint;
@synthesize locationManager;
@synthesize latLabel;
@synthesize lonLabel;
@synthesize distance;

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    self.locationManager = [[CLLocationManager alloc]init];
    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

#pragma mark -
#pragma mark CLLocationManagerDelegate Methods
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    if(startPoint==nil)
        startPoint = newLocation;
    //经度    
    NSString *lon = [[NSString alloc]initWithFormat:@"%g",newLocation.coordinate.longitude];
    self.lonLabel.text = lon;
    [lon release];
    //纬度    
    NSString *lat = [[NSString alloc]initWithFormat:@"%g",newLocation.coordinate.latitude];
    self.latLabel.text = lat;
    [lat release];
    //计算移动距离
    CLLocationDistance ld = [newLocation distanceFromLocation:startPoint];
    NSString *distanceString = [[NSString alloc]initWithFormat:@"%gm",ld];
    self.distance.text = distanceString;
    [distanceString release];
}

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSString *errorType = (error.code == kCLErrorDenied)?@"Access Denied":@"Unkown Error";
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error getting location" message:errorType delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];
    [alert release];
}

@end

       4.运行,效果如下:

        
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值