iOS开发罗盘/指南针

iphone开发/ios开发编程罗盘/指南针开发

  (2012-06-08 15:51:08)
网上很少关于指南针/罗盘的完整例子,我自己弄出来了,之前都不知道指南针所指的方向是哪个方向,指南针所指的是手机与南方的夹角,南方是磁极南还是地理南,这个可以自己设置的。手机与南方的夹角在0度-360度之间。有不懂的,百度把其他都搞懂,但是测试指南针之前必须要有真机才行
  首先创建一个view-based应用程序,我的取名是Compas
在CompasViewController.h文件中写如下代码
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface CompasViewController : UIViewController<CLLocationManagerDelegat e> {
       IBOutlet UIImageView *arrow;
       IBOutlet UILabel *angel;
       CLLocationManager *locManager;
}
@property (retain) CLLocationManager *locManager;
@property (retain,nonatomic)IBOutlet UILabel *angel;
@end
在CompasViewController.m文件中写如下代码
#define COOKBOOK_PURPLE_COLOR       [UIColor colorWithRed:0.20392f green:0.19607f blue:0.61176f alpha:1.0f]
#define BARBUTTON(TITLE, SELECTOR)        [[[UIBarButtonItem alloc] initWithTitle:TITLE style:UIBarButtonItemStylePlai n target:self action:SELECTOR] autorelease]
#import "CompasViewController.h"

@implementation CompasViewController
@synthesize locManager,angel;

- (void)dealloc
{
       [super dealloc];
}
#pragma mark - View lifecycle
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
       NSLog(@"Location manager error: %@", [error description]);
}

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
       CGFloat heading = -1.0f * M_PI * newHeading.magneticHeading / 180.0f;
       angel.text=[[NSString alloc]initWithFormat:@"angle:%f",newHeading.magneticHeading];
       arrow.transform = CGAffineTransformMakeRot ation(heading);
}

- (BOOL)locationManagerShouldDis playHeadingCalibration:(CLLocationManager *)manager
{
       return YES;
}


- (void)viewDidLoad
{
       [super viewDidLoad];
       self.navigationController.navigationBar.tintColor = COOKBOOK_PURPLE_COLOR;
     
       self.locManager = [[[CLLocationManager alloc] init] autorelease];
       self.locManager.delegate = self;
       if ([CLLocationManager headingAvailable])
              [self.locManager startUpdatingHeading];
       else
       {
               UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"atention" message:@"compass not Available" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
               [alert show];
              arrow.alpha = 0.0f;
       }
}

@end
CompasViewController.xib里面:
拖一个label和一个imageview进view,然后链接上,并把 iphone开发/ios开发编程罗盘/指南针开发
这个图片下下来,放到你的项目里面,并把imageview的image设置为这个图片,到此一切ok
这样还有问题的同学可以找我,欢迎大家相互学习,本人小菜鸟,联系方式如下:

风萧萧兮易水寒,壮士一去兮不复还。本人本是一腔抱负满身热血啊,可是最近打击连连,人生的道路上总是充满坎坷,那是各种不爽啊。譬如说,工作 上滴,那个什么其他滴啊,我也就不说了。别人是什么与什么都很顺,我是什么与什么都不顺,我就感觉这样的剧本不应该发生在我身上,况且天天我演的这么卖 力,小心哪天我心情不好了,我报复社会。呵呵,其实还不至于,还不至于。本人喜欢打DOTA,最近很想找几个兄弟开黑QQ:915893620       11平台号:cscin1016。其实说实在的,我也觉得我也当演员的天赋,哎。。只是天不遂人愿啊。。。生不逢时。。。


源码下载地址http://www.kuaipan.cn/file/id_30491149655343763.htm

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值