Skip to content

Commit 61d7be6

Browse files
author
UQ Times
committed
ヘディングのサンプル追加
1 parent 9a8f033 commit 61d7be6

File tree

5 files changed

+972
-27
lines changed

5 files changed

+972
-27
lines changed

CoreLocationSample-HeadingRelated/CoreLocationSample-HeadingRelated.xcodeproj/project.pbxproj

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
CE70AE2E15704A7B006F382A /* CoreLocation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CE70AE2D15704A7B006F382A /* CoreLocation.framework */; };
1011
CEF7B03B156F448C00BADCD4 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CEF7B03A156F448C00BADCD4 /* UIKit.framework */; };
1112
CEF7B03D156F448C00BADCD4 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CEF7B03C156F448C00BADCD4 /* Foundation.framework */; };
1213
CEF7B03F156F448C00BADCD4 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CEF7B03E156F448C00BADCD4 /* CoreGraphics.framework */; };
@@ -19,6 +20,7 @@
1920
/* End PBXBuildFile section */
2021

2122
/* Begin PBXFileReference section */
23+
CE70AE2D15704A7B006F382A /* CoreLocation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreLocation.framework; path = System/Library/Frameworks/CoreLocation.framework; sourceTree = SDKROOT; };
2224
CEF7B036156F448C00BADCD4 /* CoreLocationSample-HeadingRelated.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "CoreLocationSample-HeadingRelated.app"; sourceTree = BUILT_PRODUCTS_DIR; };
2325
CEF7B03A156F448C00BADCD4 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
2426
CEF7B03C156F448C00BADCD4 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
@@ -40,6 +42,7 @@
4042
isa = PBXFrameworksBuildPhase;
4143
buildActionMask = 2147483647;
4244
files = (
45+
CE70AE2E15704A7B006F382A /* CoreLocation.framework in Frameworks */,
4346
CEF7B03B156F448C00BADCD4 /* UIKit.framework in Frameworks */,
4447
CEF7B03D156F448C00BADCD4 /* Foundation.framework in Frameworks */,
4548
CEF7B03F156F448C00BADCD4 /* CoreGraphics.framework in Frameworks */,
@@ -69,6 +72,7 @@
6972
CEF7B039156F448C00BADCD4 /* Frameworks */ = {
7073
isa = PBXGroup;
7174
children = (
75+
CE70AE2D15704A7B006F382A /* CoreLocation.framework */,
7276
CEF7B03A156F448C00BADCD4 /* UIKit.framework */,
7377
CEF7B03C156F448C00BADCD4 /* Foundation.framework */,
7478
CEF7B03E156F448C00BADCD4 /* CoreGraphics.framework */,
@@ -216,11 +220,12 @@
216220
"$(inherited)",
217221
);
218222
GCC_SYMBOLS_PRIVATE_EXTERN = NO;
223+
GCC_THUMB_SUPPORT = NO;
219224
GCC_VERSION = com.apple.compilers.llvm.clang.1_0;
220225
GCC_WARN_ABOUT_RETURN_TYPE = YES;
221226
GCC_WARN_UNINITIALIZED_AUTOS = YES;
222227
GCC_WARN_UNUSED_VARIABLE = YES;
223-
IPHONEOS_DEPLOYMENT_TARGET = 5.1;
228+
IPHONEOS_DEPLOYMENT_TARGET = 4.0;
224229
SDKROOT = iphoneos;
225230
TARGETED_DEVICE_FAMILY = "1,2";
226231
};
@@ -234,11 +239,12 @@
234239
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
235240
COPY_PHASE_STRIP = YES;
236241
GCC_C_LANGUAGE_STANDARD = gnu99;
242+
GCC_THUMB_SUPPORT = NO;
237243
GCC_VERSION = com.apple.compilers.llvm.clang.1_0;
238244
GCC_WARN_ABOUT_RETURN_TYPE = YES;
239245
GCC_WARN_UNINITIALIZED_AUTOS = YES;
240246
GCC_WARN_UNUSED_VARIABLE = YES;
241-
IPHONEOS_DEPLOYMENT_TARGET = 5.1;
247+
IPHONEOS_DEPLOYMENT_TARGET = 4.0;
242248
OTHER_CFLAGS = "-DNS_BLOCK_ASSERTIONS=1";
243249
SDKROOT = iphoneos;
244250
TARGETED_DEVICE_FAMILY = "1,2";
@@ -252,6 +258,7 @@
252258
GCC_PRECOMPILE_PREFIX_HEADER = YES;
253259
GCC_PREFIX_HEADER = "CoreLocationSample-HeadingRelated/CoreLocationSample-HeadingRelated-Prefix.pch";
254260
INFOPLIST_FILE = "CoreLocationSample-HeadingRelated/CoreLocationSample-HeadingRelated-Info.plist";
261+
IPHONEOS_DEPLOYMENT_TARGET = 4.0;
255262
PRODUCT_NAME = "$(TARGET_NAME)";
256263
WRAPPER_EXTENSION = app;
257264
};
@@ -263,6 +270,7 @@
263270
GCC_PRECOMPILE_PREFIX_HEADER = YES;
264271
GCC_PREFIX_HEADER = "CoreLocationSample-HeadingRelated/CoreLocationSample-HeadingRelated-Prefix.pch";
265272
INFOPLIST_FILE = "CoreLocationSample-HeadingRelated/CoreLocationSample-HeadingRelated-Info.plist";
273+
IPHONEOS_DEPLOYMENT_TARGET = 4.0;
266274
PRODUCT_NAME = "$(TARGET_NAME)";
267275
WRAPPER_EXTENSION = app;
268276
};
@@ -287,6 +295,7 @@
287295
CEF7B059156F448C00BADCD4 /* Release */,
288296
);
289297
defaultConfigurationIsVisible = 0;
298+
defaultConfigurationName = Release;
290299
};
291300
/* End XCConfigurationList section */
292301
};

CoreLocationSample-HeadingRelated/CoreLocationSample-HeadingRelated/CoreLocationSample-HeadingRelated-Info.plist

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,10 @@
3131
<key>UISupportedInterfaceOrientations</key>
3232
<array>
3333
<string>UIInterfaceOrientationPortrait</string>
34-
<string>UIInterfaceOrientationLandscapeLeft</string>
35-
<string>UIInterfaceOrientationLandscapeRight</string>
3634
</array>
3735
<key>UISupportedInterfaceOrientations~ipad</key>
3836
<array>
3937
<string>UIInterfaceOrientationPortrait</string>
40-
<string>UIInterfaceOrientationPortraitUpsideDown</string>
41-
<string>UIInterfaceOrientationLandscapeLeft</string>
42-
<string>UIInterfaceOrientationLandscapeRight</string>
4338
</array>
4439
</dict>
4540
</plist>

CoreLocationSample-HeadingRelated/CoreLocationSample-HeadingRelated/ViewController.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,21 @@
77
//
88

99
#import <UIKit/UIKit.h>
10+
#import <CoreLocation/CoreLocation.h>
1011

11-
@interface ViewController : UIViewController
12+
@interface ViewController : UIViewController <CLLocationManagerDelegate>
13+
14+
@property (retain, nonatomic) IBOutlet UILabel *headingFilterLabel;
15+
@property (retain, nonatomic) IBOutlet UILabel *headingOrientationLabel;
16+
@property (retain, nonatomic) IBOutlet UILabel *headingAccuracyLabel;
17+
@property (retain, nonatomic) IBOutlet UILabel *magneticHeadingLabel;
18+
@property (retain, nonatomic) IBOutlet UILabel *trueHeadingLabel;
19+
@property (retain, nonatomic) IBOutlet UILabel *xLabel;
20+
@property (retain, nonatomic) IBOutlet UILabel *yLabel;
21+
@property (retain, nonatomic) IBOutlet UILabel *zLabel;
22+
@property (nonatomic, retain) IBOutlet UILabel *arrowLabel;
23+
@property (retain, nonatomic) IBOutlet UISegmentedControl *northSegment;
24+
25+
- (IBAction)northSegmentChanged:(UISegmentedControl *)sender;
1226

1327
@end

CoreLocationSample-HeadingRelated/CoreLocationSample-HeadingRelated/ViewController.m

Lines changed: 167 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,189 @@
99
#import "ViewController.h"
1010

1111
@interface ViewController ()
12-
12+
@property (nonatomic, retain) CLLocationManager *locationManager;
1313
@end
1414

1515
@implementation ViewController
1616

17+
@synthesize locationManager = _locationManager;
18+
19+
@synthesize headingFilterLabel = _headingFilterLabel;
20+
@synthesize headingOrientationLabel = _headingOrientationLabel;
21+
@synthesize headingAccuracyLabel = _headingAccuracyLabel;
22+
@synthesize magneticHeadingLabel = _magneticHeadingLabel;
23+
@synthesize trueHeadingLabel = _trueHeadingLabel;
24+
@synthesize xLabel = _xLabel;
25+
@synthesize yLabel = _yLabel;
26+
@synthesize zLabel = _zLabel;
27+
@synthesize arrowLabel = _arrowLabel;
28+
@synthesize northSegment = _northSegment;
29+
1730
- (void)viewDidLoad
1831
{
1932
[super viewDidLoad];
20-
// Do any additional setup after loading the view, typically from a nib.
33+
34+
// ヘディングの設定
35+
CLLocationDegrees headingFilter = kCLHeadingFilterNone; // 常に
36+
CLDeviceOrientation headingOrientation = CLDeviceOrientationPortrait; // 上部を基準とする
37+
38+
// インスタンスの生成
39+
_locationManager = [[CLLocationManager alloc] init];
40+
// ヘディングが有効ならば設定をして開始
41+
if ([CLLocationManager headingAvailable]) {
42+
_locationManager.delegate = self;
43+
_locationManager.headingFilter = headingFilter;
44+
_locationManager.headingOrientation = headingOrientation;
45+
[_locationManager startUpdatingHeading];
46+
47+
// Viewのスイッチの設定
48+
self.northSegment.selectedSegmentIndex = 1; // 磁北を選択
49+
self.northSegment.enabled = YES;
50+
}
51+
52+
/* Viewに反映 */
53+
self.headingFilterLabel.text = [NSString stringWithFormat:@"%lf", headingFilter];
54+
self.headingOrientationLabel.text = [self deviceOrientationToString:headingOrientation];
2155
}
2256

2357
- (void)viewDidUnload
2458
{
59+
_locationManager.delegate = nil;
60+
61+
self.locationManager = nil;
62+
self.headingFilterLabel = nil;
63+
self.headingOrientationLabel = nil;
64+
self.headingAccuracyLabel = nil;
65+
self.magneticHeadingLabel = nil;
66+
self.trueHeadingLabel = nil;
67+
self.xLabel = nil;
68+
self.yLabel = nil;
69+
self.zLabel = nil;
70+
self.arrowLabel = nil;
71+
self.northSegment = nil;
72+
2573
[super viewDidUnload];
26-
// Release any retained subviews of the main view.
74+
}
75+
76+
- (void)dealloc
77+
{
78+
_locationManager.delegate = nil;
79+
80+
[_locationManager release];
81+
[_headingFilterLabel release];
82+
[_headingOrientationLabel release];
83+
[_headingAccuracyLabel release];
84+
[_magneticHeadingLabel release];
85+
[_trueHeadingLabel release];
86+
[_xLabel release];
87+
[_yLabel release];
88+
[_zLabel release];
89+
[_northSegment release];
90+
91+
[super dealloc];
2792
}
2893

2994
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
3095
{
31-
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
32-
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
33-
} else {
34-
return YES;
96+
return (interfaceOrientation == UIInterfaceOrientationPortrait);
97+
}
98+
99+
/* セグメントが変更された */
100+
- (IBAction)northSegmentChanged:(UISegmentedControl *)sender {
101+
switch (sender.selectedSegmentIndex) {
102+
case 0: // 停止
103+
NSLog(@"%s | stopUpdatingLocation & stopUpdatingHeading", __PRETTY_FUNCTION__);
104+
[_locationManager stopUpdatingLocation];
105+
[_locationManager stopUpdatingHeading];
106+
break;
107+
case 1: // 磁北
108+
NSLog(@"%s | stopUpdatingLocation & startUpdatingHeading", __PRETTY_FUNCTION__);
109+
[_locationManager stopUpdatingLocation];
110+
[_locationManager startUpdatingHeading];
111+
break;
112+
case 2: // 真北
113+
NSLog(@"%s | startUpdatingLocation & startUpdatingHeading", __PRETTY_FUNCTION__);
114+
_locationManager.distanceFilter = kCLDistanceFilterNone;
115+
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
116+
[_locationManager startUpdatingLocation];
117+
[_locationManager startUpdatingHeading];
118+
break;
119+
}
120+
}
121+
122+
#pragma mark - CLLocationManagerDelegate methods
123+
124+
/* ヘディング情報が更新された */
125+
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
126+
{
127+
if (newHeading.headingAccuracy < 0) {
128+
return; // 正しく取得できていない
129+
}
130+
131+
// 磁北か真北か
132+
CLLocationDirection heading = (self.northSegment.selectedSegmentIndex == 1) ? newHeading.magneticHeading : newHeading.trueHeading;
133+
// 値が正しくなければ0度で固定
134+
if (heading < 0) {
135+
heading = 0;
136+
}
137+
138+
/* Viewに反映 */
139+
CGFloat radius = (heading / 180) * M_PI;
140+
self.arrowLabel.transform = CGAffineTransformMakeRotation(-radius); // 回転方向が逆なので符号を反転
141+
142+
self.headingAccuracyLabel.text = [NSString stringWithFormat:@"%lf", newHeading.headingAccuracy];
143+
self.magneticHeadingLabel.text = [NSString stringWithFormat:@"%lf", newHeading.magneticHeading];
144+
self.trueHeadingLabel.text = [NSString stringWithFormat:@"%lf", newHeading.trueHeading];
145+
self.xLabel.text = [NSString stringWithFormat:@"%lf", newHeading.x];
146+
self.yLabel.text = [NSString stringWithFormat:@"%lf", newHeading.y];
147+
self.zLabel.text = [NSString stringWithFormat:@"%lf", newHeading.z];
148+
}
149+
150+
/* コンパスの調整を画面に表示するかどうか */
151+
- (BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)manager
152+
{
153+
return YES;
154+
}
155+
156+
/* 下部スイッチの変更のため、位置情報サービスのオン・オフの変更を受け取る */
157+
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
158+
{
159+
if ([CLLocationManager headingAvailable]) {
160+
// ヘディングが有効な場合のみ
161+
if (status == kCLAuthorizationStatusAuthorized || status == kCLAuthorizationStatusNotDetermined) {
162+
// 真北を有効化
163+
[self.northSegment setEnabled:YES forSegmentAtIndex:2];
164+
} else {
165+
// それ以外は無効化
166+
if (self.northSegment.selectedSegmentIndex == 2) {
167+
// 真北が選ばれていたならば、磁北に移動
168+
self.northSegment.selectedSegmentIndex = 1;
169+
}
170+
[self.northSegment setEnabled:NO forSegmentAtIndex:2];
171+
}
172+
}
173+
}
174+
175+
#pragma mark - private methods
176+
177+
/* デバイスの向きを文字列に変換する */
178+
- (NSString *)deviceOrientationToString:(CLDeviceOrientation)orientation
179+
{
180+
switch (orientation) {
181+
case CLDeviceOrientationUnknown:
182+
return @"Unknown";
183+
case CLDeviceOrientationPortrait:
184+
return @"Portrait";
185+
case CLDeviceOrientationPortraitUpsideDown:
186+
return @"UpsideDown";
187+
case CLDeviceOrientationLandscapeLeft:
188+
return @"LandscapeLeft";
189+
case CLDeviceOrientationLandscapeRight:
190+
return @"LandscapeRight";
191+
case CLDeviceOrientationFaceUp:
192+
return @"FaceUp";
193+
case CLDeviceOrientationFaceDown:
194+
return @"FaceDown";
35195
}
36196
}
37197

0 commit comments

Comments
 (0)