Skip to content

Commit 8eb9ac4

Browse files
authored
animateTo support reverse Rotation direction (#2414)
* animateTo support reverse Rotation direction * spec * update spec * updates
1 parent 8cdaea8 commit 8eb9ac4

File tree

4 files changed

+257
-30
lines changed

4 files changed

+257
-30
lines changed

src/map/Map.Anim.ts

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Animation, Player } from '../core/Animation';
22
import Coordinate from '../geo/Coordinate';
33
import Point from '../geo/Point';
4-
import Map from './Map';
4+
import { Map, MapViewType } from './Map';
55
import { isNil, isFunction, hasOwn, extend, clamp } from '../core/util';
66

77

@@ -36,6 +36,40 @@ declare module "./Map" {
3636
// return true;
3737
// }
3838

39+
function needValidateView(view: MapViewType, map: Map) {
40+
const bearing = view.bearing;
41+
const currentBearing = map.getBearing();
42+
//当bearing溢出半角时,且线型变化时不经过0时,例如:[currentBearing=170,bearing=220],[currentBearing=-170,bearing=-220]
43+
if (currentBearing >= 0 && bearing > 180) {
44+
return false;
45+
}
46+
if (currentBearing <= 0 && bearing < -180) {
47+
return false;
48+
}
49+
return true;
50+
}
51+
//反向旋转,bearing的变化正常为 [a,b],开启reverse后变化就反向了,例如 [170,-170] reverse后为 [170,190]
52+
function reverseBearing(view: MapViewType, map: Map) {
53+
const bearing = view.bearing;
54+
const currentBearing = map.getBearing();
55+
//such as [170,-170]
56+
if (currentBearing >= 0 && bearing < 0) {
57+
view.bearing = 180 + (180 - Math.abs(bearing));
58+
}
59+
//such as [-170,170]
60+
if (currentBearing <= 0 && bearing > 0) {
61+
view.bearing = -180 - (180 - Math.abs(bearing));
62+
}
63+
64+
if (currentBearing >= 0 && bearing > 0) {
65+
view.bearing = -180 - (180 - Math.abs(bearing));
66+
}
67+
68+
if (currentBearing <= 0 && bearing < 0) {
69+
view.bearing = 180 + (180 - Math.abs(bearing));
70+
}
71+
}
72+
3973
Map.include(/** @lends Map.prototype */{
4074

4175
/**
@@ -63,12 +97,18 @@ Map.include(/** @lends Map.prototype */{
6397
*/
6498
animateTo(view, options = {}, step) {
6599
view = extend({}, this.getView(), view);
66-
this._validateView(view);
100+
view.bearing = view.bearing % 360;
67101
// this._stopAnim(this._animPlayer);
68102
if (isFunction(options)) {
69103
step = options;
70104
options = {};
71105
}
106+
if ((options as any).counterclockwise) {
107+
reverseBearing(view, this);
108+
}
109+
if (needValidateView(view, this)) {
110+
this._validateView(view);
111+
}
72112
const projection = this.getProjection(),
73113
currView = this.getView(),
74114
props = {};
@@ -228,7 +268,13 @@ Map.include(/** @lends Map.prototype */{
228268
// Where applicable, local variable documentation begins with the associated variable or
229269
// function in van Wijk (2003).
230270
view = extend({}, this.getView(), view);
231-
this._validateView(view);
271+
view.bearing = view.bearing % 360;
272+
if ((options as any).counterclockwise) {
273+
reverseBearing(view, this);
274+
}
275+
if (needValidateView(view, this)) {
276+
this._validateView(view);
277+
}
232278

233279
if (this._animPlayer) {
234280
if (this._isInternalAnimation) {

src/map/Map.Camera.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,8 @@ Map.include(/** @lends Map.prototype */{
354354
*/
355355
setCameraOrientation(params) {
356356
const { position } = params;
357-
let { pitch, bearing } = params;
358357
this._validateView(params);
358+
let { pitch, bearing } = params;
359359
pitch = pitch || 0;
360360
bearing = bearing || 0;
361361
const { zoom, cameraToGroundDistance } = this.getFitZoomForCamera(position, pitch);

src/map/Map.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,8 +1045,21 @@ export class Map extends Handlerable(Eventable(Renderable(Class))) {
10451045
return;
10461046
}
10471047
if (isNumber(view.bearing)) {
1048-
view.bearing = Math.max(-180, view.bearing);
1049-
view.bearing = Math.min(180, view.bearing);
1048+
let bearing = view.bearing;
1049+
//周期性
1050+
bearing = bearing % 360;
1051+
//自动转换为负的值
1052+
if (bearing > 180) {
1053+
bearing = -180 + Math.abs(bearing - 180);
1054+
}
1055+
//自动转换为正的值
1056+
if (bearing < -180) {
1057+
bearing = 180 - Math.abs(bearing + 180);
1058+
}
1059+
view.bearing = bearing;
1060+
1061+
// view.bearing = Math.max(-180, view.bearing);
1062+
// view.bearing = Math.min(180, view.bearing);
10501063
}
10511064
if (isNumber(view.pitch)) {
10521065
view.pitch = Math.max(0, view.pitch);
@@ -2829,7 +2842,7 @@ export type MapDataURLType = {
28292842
save?: boolean;
28302843
}
28312844

2832-
export type MapAnimationOptionsType = AnimationOptionsType;
2845+
export type MapAnimationOptionsType = AnimationOptionsType & { counterclockwise?: boolean }
28332846

28342847
export type MapIdentifyOptionsType = {
28352848
tolerance?: number;

0 commit comments

Comments
 (0)