你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iPhone開發之CoreLocation定位功能(6)

iPhone開發之CoreLocation定位功能(6)

編輯:IOS開發綜合

學了iPhone的CoreLocation之後,再回想Android的定位開發,真是省事了不少,iPhone對定位功能開發這一模塊封裝的很好,只需幾步,便可以獲取到設備所在的位置等多項參數!
        1.啟動XCode4.3.2,單擊菜單項File->New->Project...,以Sigle View Application模板新建項目,並命名為WhereAmI:
       
        2.單擊ViewControler.h頭文件,因為CoreLocation框架並不屬於UIKit框架,所以需要另外引入,並添加協議:
[plain] 
#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文件,添加以下代碼:
[plain]
#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.運行,效果如下:



作者:js_dada
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved