【3D技术宅公社】XR数字艺术论坛  XR技术讨论 XR互动电影 定格动画

 找回密码
 立即注册

QQ登录

只需一步,快速开始

调查问卷
论坛即将给大家带来全新的技术服务,面向三围图形学、游戏、动画的全新服务论坛升级为UTF8版本后,中文用户名和用户密码中有中文的都无法登陆,请发邮件到324007255(at)QQ.com联系手动修改密码

3D技术论坛将以计算机图形学为核心,面向教育 推出国内的三维教育引擎该项目在持续研发当中,感谢大家的关注。

查看: 2657|回复: 0

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

[复制链接]
发表于 2012-12-23 01:23:13 | 显示全部楼层 |阅读模式
学了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        
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|3D数字艺术论坛 ( 沪ICP备14023054号 )

GMT+8, 2024-11-23 12:45

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表