• 主页
  • 系列总集
  • OpenCV
  • CMake
  • iOS
  • Java
  • 前端
所有文章 关于我

  • 主页
  • 系列总集
  • OpenCV
  • CMake
  • iOS
  • Java
  • 前端

MKMapKit(七):指南针追踪工具

2017-03-09

Compass.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#import <UIKit/UIKit.h>
#import "MapKit.h"

@interface MapKit (Compass)

/**
* 在MapKit的Tracking模式中的CameraHeading模式下计算self.camera.heading应该赋值多少
* @param degrees LocationManager的Heading角度
* @return
*/
+ (CLLocationDirection)cameraHeadingInCameraHeadingModeByDegrees:(CLLocationDegrees)degrees;

/**
* 1. 在MapKit的Tracking模式中的TransformHeading模式下,应该如何转动MapKit的Transform保证其TransformIdentity始终指向正北方0度
* 2. 用于在不同的Tracking模式下,View如果想锁定指向时的辅助函数
* @param degrees LocationManager的Heading角度
*/
+ (CATransform3D)transformInTransformHeadingModeByDegrees:(CLLocationDegrees)degrees;

/**
* 在不同的Tracking模式下,保证View始终指向锁定的角度
* @param heading 锁定的角度 0 - 360,其中0为正北方
* @param mapHeading 当前地图的角度,一般为LocationManage刷新的设备角度
* @param mode 当前MapKit使用的追踪模式
* @return
*/
+ (CATransform3D)keepHeadingDegrees:(CLLocationDirection)heading withLocationHeading:(CLLocationDirection)mapHeading inMode:(MapKitTrackingMode)mode;

/**
* 在不开启Tracking的模式下(),在对MapKit进行了RotateGesture的情况下,UIView锁定指向
* @param heading 锁定的角度 0 - 360
* @param cameraHeading 当前MapKit的摄像头角度,self.camera.heading 在Rotate了地图之后该参数会变化
* @return
*/
+ (CATransform3D)keepTrackingNoneModeHeadingDegrees:(CLLocationDegrees)heading withCameraHeading:(CLLocationDirection)cameraHeading;

@end

Compass.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185

#import "MapKit+Compass.h"

/**
*
* 以 !!!2014年旋转Transform的Map例子中 !!!!分析
*
* 首先假设设备处于UIInterfaceOrientationPortrait
* 指南针度数 偏转角度 +360度至递减
* 135 -135 225
* 225 135 135
* 解方程
* degrees = 360 - degrees;
*
* 在UIInterfaceOrientationLandscapeRight情况下
* 指南针度数 偏转角度 +360度至递减
* 0 -90 270
* 45 -135 225
* 90 -180 180
*!!! 120 150 150
* 135 135 135
* 180 90 90
* 225 45 45
* 270 0 0
* 315 -45 -45
* 360 -90 -90
* 使用二元一次方程组 y = ax+b
* 观察数据可得
* 偏转角度 = -指南针度数 + 270
* 感谢 Ye.Tao-2 提供的计算能力
* 解方程
* 可得 degrees = 270-degrees;
* 即 Portrait 向左倾倒,设备旋转了 -90度 360 - degrees -90
*
*
*
* 在UIInterfaceOrientationLandscapeLeft
* 指南针度数 偏转角度 +360度至递减
* 225 -135 225
* 270 -180 180
* 315 135 135
* 解方程
* 可得 degrees = 450-degrees;
* 即 Portrait 向右倾倒,设备旋转了 -90度 360 - degrees -90
*
*
*
* 在 UIDeviceOrientationPortraitUpsideDown
* 即 Portrait 向右倾倒旋转180度(360 - degrees + 180) ,或者向左倾倒旋转-180度(360 -degrees - 180)
*
*
*
*/

@implementation MapKit (Compass)


+ (CLLocationDirection)cameraHeadingInCameraHeadingModeByDegrees:(CLLocationDegrees)degrees {
CLLocationDirection temp = [self degreesRangeProcess:degrees];
//首先假设设备处于UIInterfaceOrientationPortrait
if(UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
temp = 180 + temp;
}
//处理设备旋转
temp = [self deviceOrientationProcess:temp];

return temp;
}

+ (CATransform3D)transformInTransformHeadingModeByDegrees:(CLLocationDegrees)degrees {
CLLocationDirection temp = [self degreesRangeProcess:degrees];
//首先假设设备处于UIInterfaceOrientationPortrait
temp = 360 - temp;
//处理设备旋转
temp = [self deviceOrientationProcess:temp];

return CATransform3DMakeRotation([self degreesToRadians:temp], 0, 0, 1);
}

+ (CATransform3D)keepHeadingDegrees:(CLLocationDirection)heading withLocationHeading:(CLLocationDirection)mapHeading inMode:(MapKitTrackingMode)mode {
switch (mode) {
case MapKitTrackingModeFollowWithHeading:
return [self keepFellowModeHeadingDegrees:heading withMapHeading:mapHeading];
case MapKitTrackingModeCameraHeading:
return [self keepCameraModeHeadingDegrees:heading withMapHeading:mapHeading];
case MapKitTrackingModeTransformHeading:
return CATransform3DMakeRotation([self degreesToRadians:heading], 0, 0, 1);
default:
break;
}
return CATransform3DIdentity;
}

+ (CATransform3D)keepTrackingNoneModeHeadingDegrees:(CLLocationDirection)heading withCameraHeading:(CLLocationDirection)cameraHeading {
CLLocationDegrees temp = [self deviceOrientationProcess:cameraHeading];
return [self keepCameraModeHeadingDegrees:heading withMapHeading:temp];

}

#pragma mark - Logic Funcation

+ (CATransform3D)keepFellowModeHeadingDegrees:(CLLocationDirection)heading withMapHeading:(CLLocationDirection)mapHeading {
/**
* 在MapKitTrackingModeFollowWithHeading模式下
* heading度数x CATransform3DIdentity指向地图角度 偏移量y
* 0 0 360
* 45 -45 315
* 90 -90 270
* 135 -135 225
* 180 180 180
* 225 135 135
* 270 90 90
* 315 45 45
* 360 0 0
*/
mapHeading = - heading + mapHeading;
return [self transformInTransformHeadingModeByDegrees:mapHeading];
}

+ (CATransform3D)keepCameraModeHeadingDegrees:(CLLocationDirection)heading withMapHeading:(CLLocationDirection)mapHeading {
/**
* 在MapKitTrackingModeCameraHeading模式下
* heading度数x CATransform3DIdentity指向地图角度 偏移量y
* 0 0 360
* 45 -45 315
* 90 -90 270
* 135 -135 225
* 180 180 180
* 225 135 135
* 270 90 90
* 315 45 45
* 360 0 0
*/
mapHeading = -heading + mapHeading;
return [self transformInTransformHeadingModeByDegrees:mapHeading];
}

#pragma mark - Associate Funcation

+ (CLLocationDegrees)degreesRangeProcess:(CLLocationDegrees)degrees {
CLLocationDirection temp = degrees;
while (temp > 360) {
temp -= 360;
}
while (temp < 0) {
temp += 360;
}
return temp;
}

+ (CLLocationDegrees)deviceOrientationProcess:(CLLocationDegrees)degrees {
/**
* 首先假设设备处于UIInterfaceOrientationPortrait
*/
CLLocationDirection temp = degrees;
#if !TARGET_OS_SIMULATOR
if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight) {
/**
* 在UIInterfaceOrientationLandscapeRight情况下
* 即 Portrait 向左倾倒,设备旋转了 -90度 degrees -90
*/
temp = temp - 90;
} else if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft) {
/**
* 在UIInterfaceOrientationLandscapeLeft
* 即 Portrait 向右倾倒,设备旋转了 +90度 degrees +90
*/
temp = temp + 90;
} else if ([[UIApplication sharedApplication] statusBarOrientation] == UIDeviceOrientationPortraitUpsideDown) {
/**
* 在 UIDeviceOrientationPortraitUpsideDown
* 即 Portrait 向右倾倒旋转180度(degrees + 180) ,或者向左倾倒旋转-180度(degrees - 180)
*/
temp = temp + 180;
}
#endif
return temp;
}

+ (CGFloat)degreesToRadians:(CLLocationDegrees)degrees {
return degrees*M_PI/180.0f;
}

@end

赏

请问老板还招人么(/ω\)

支付宝
微信
  • iOS
  • MKMapKit
  • Tutorial

扫一扫,分享到微信

微信分享二维码
MKMapKit(八):动态Overlay绘制
MKMapKit(六):指南针模式
© 2021 Alan Li
Hexo Theme Yilia by Litten
  • 所有文章
  • 关于我

tag:

  • iOS
  • Java
  • Collection
  • Python
  • Shell
  • CMake
  • Memory
  • JavaScript
  • Architecture
  • AnchorPoint
  • Android
  • Web
  • Annotation
  • AFNetworking
  • Window
  • ViewController
  • AutoLayout
  • Dozer
  • CoreAnimation
  • Cycle Retain
  • Block
  • UI
  • IDE
  • FrontEnd
  • CSS
  • Category
  • TableViewCell
  • Security
  • Net
  • JSP
  • Spring
  • C
  • MyBatis
  • Date
  • React
  • GCD
  • UITouch
  • Gesture
  • UIControl
  • Git
  • HTML
  • HTTPS
  • HTTP
  • Servlet
  • Server
  • DataBase
  • MySQL
  • Linux
  • Tutorial
  • Ajax
  • Type
  • JQuery
  • JSON
  • Exception
  • Parameter
  • Reflect
  • Thread
  • Sort
  • KVO
  • MKMapKit
  • Overlay
  • Maven
  • Configure
  • Tips
  • Transaction
  • Swift
  • NavigationBar
  • Nginx
  • Runtime
  • OpenCV
  • Property
  • Playground
  • Protocol
  • Redux
  • ScrollView
  • Session
  • Cookie
  • Shiro
  • Error
  • Singleton
  • RegEx
  • StackView
  • StatusBar
  • Base64
  • Socket
  • TCP
  • IP
  • TextField
  • CALayer
  • UILabel
  • View
  • Animation
  • Xcode
  • Hexo
  • Terminal
  • OC
  • Device
  • Log
  • Image
  • JUnit
  • Oval
  • Archive
  • XSS
  • Compiler
  • Aspect
  • Responder
  • Class
  • FireWall
  • RetainCount
  • Const
  • Frame
  • String
  • Symbols
  • Framework
  • CocoaPods
  • Unity
  • Message
  • Button
  • AuthorizationStatus
  • Struct
  • XCTest
  • NSNotification
  • Contact

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

我写的,大概率是错的。。。。。