一:指南針
#import <CoreLocation/CoreLocation.h>
CLLocationManager *locManger = [[CLLocationManager alloc] init];
locManger.delegate = self;
locManger.headingFilter = 0.1;// 最小變化度數(shù)
[locManger startUpdatingHeading];// 開始更新方向
// 代理方法
- (void)locationManager:(CLLocationManager*)manager didUpdateHeading:(CLHeading*)newHeading {
? ? if(newHeading.magneticHeading == 0) {// 0度表示北方,180度表示南方
? ? ? ? NSLog(@"北方");
? ? } else if(newHeading.magneticHeading == 180) {
? ? NSLog(@"南方");
}}
二:GPS
#import <CoreLocation/CoreLocation.h>
CLLocationManager *locManger = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;// 設(shè)置委托
[self.locationManager setDesiredAccuracy:CLLocationDistanceMax];// 設(shè)置精度為最優(yōu)
[self.locationManager setDistanceFilter:(100.0)];// 設(shè)置更新的最小距離100米
[self.locationManager startUpdatingLocation];// 開始更新位置信息
回調(diào)方法
- (void)locationManager:(CLLocationManager*)manager
didUpdateLocations:(NSArray*)locations {
CLLocation *nowLocation = [locations lastObject];// 獲取當(dāng)前位置
CLLocationDegrees longitutude = newLocation.coordinate.longitude;// 獲取經(jīng)度
CLLocationDegrees latitude = newLocation.coordinate.latitude;// 獲取緯度
}
// 計(jì)算距離
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { ?
double distance = [newLocation distanceFromLocation:oldLocation]; ? NSLog(@"distance = %lf", distance);
}
三:加速計(jì)
1.獲得加速計(jì)對(duì)象
UIAccelerometer*accelerometer = [UIAccelerometersharedAccelerometer];
accelerometer.delegate=self;// 設(shè)置委托
// 設(shè)置更新間隔時(shí)間
accelerometer.updateInterval=1.0f/30.0f;
2.回調(diào)方法
- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration {
? self.xLabel.text= [NSString stringWithFormat:@"%f",acceleration.x];// 獲得x軸原始數(shù)據(jù)
? self.yLabel= [NSString stringWithFormat:@"%f",acceleration.y];// 獲得y軸原始數(shù)據(jù)
? self.zLabel= [NSString stringWithFormat:@"%f",acceleration.z];// 獲得z軸原始數(shù)據(jù)
}
四:搖一搖
// 在Controller中重寫此方法,就可以獲取搖一搖事件
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent*)event {
? if (motion == UIEventSubtypeMotionShake) {
? // 檢測(cè)到了,手機(jī)搖一搖
}}