aboutsummaryrefslogtreecommitdiffstats
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraUpdateFactory.java
blob: c4cc6d01962bd14987870fd4cb77aaffac3bc77d (plain)
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
package com.mapbox.mapboxsdk.camera;

import android.graphics.Point;
import android.graphics.PointF;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;

import android.support.annotation.Nullable;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.Projection;
import com.mapbox.mapboxsdk.maps.UiSettings;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Arrays;

/**
 * Factory for creating CameraUpdate objects.
 */
public final class CameraUpdateFactory {

  /**
   * Returns a CameraUpdate that moves the camera to a specified CameraPosition.
   *
   * @param cameraPosition Camera Position to change to
   * @return CameraUpdate Final Camera Position
   */
  public static CameraUpdate newCameraPosition(@NonNull CameraPosition cameraPosition) {
    return new CameraPositionUpdate(cameraPosition.bearing, cameraPosition.target, cameraPosition.tilt,
      cameraPosition.zoom);
  }

  /**
   * Returns a CameraUpdate that moves the center of the screen to a latitude and longitude
   * specified by a LatLng object. This centers the camera on the LatLng object.
   *
   * @param latLng Target location to change to
   * @return CameraUpdate Final Camera Position
   */
  public static CameraUpdate newLatLng(@NonNull LatLng latLng) {
    return new CameraPositionUpdate(-1, latLng, -1, -1);
  }

  /**
   * Returns a CameraUpdate that transforms the camera such that the specified
   * latitude/longitude bounds are centered on screen at the greatest possible zoom level.
   * You can specify padding, in order to inset the bounding box from the map view's edges.
   * The returned CameraUpdate has a bearing of 0 and a tilt of 0.
   *
   * @param bounds  Bounds to match Camera position with
   * @param padding Padding added to the bounds
   * @return CameraUpdate Final Camera Position
   */
  public static CameraUpdate newLatLngBounds(@NonNull LatLngBounds bounds, int padding) {
    return newLatLngBounds(bounds, padding, padding, padding, padding);
  }

  /**
   * Returns a CameraUpdate that transforms the camera such that the specified
   * latitude/longitude bounds are centered on screen at the greatest possible zoom level.
   * You can specify padding, in order to inset the bounding box from the map view's edges.
   * The returned CameraUpdate has a bearing of 0 and a tilt of 0.
   *
   * @param bounds        Bounds to base the Camera position out of
   * @param paddingLeft   Padding left of the bounds
   * @param paddingTop    Padding top of the bounds
   * @param paddingRight  Padding right of the bounds
   * @param paddingBottom Padding bottom of the bounds
   * @return CameraUpdate Final Camera Position
   */
  public static CameraUpdate newLatLngBounds(@NonNull LatLngBounds bounds, int paddingLeft, int paddingTop,
                                             int paddingRight, int paddingBottom) {
    return new CameraBoundsUpdate(bounds, paddingLeft, paddingTop, paddingRight, paddingBottom);
  }

  /**
   * Returns a CameraUpdate that moves the center of the screen to a latitude and longitude
   * specified by a LatLng object, and moves to the given zoom level.
   *
   * @param latLng Target location to change to
   * @param zoom   Zoom level to change to
   * @return CameraUpdate Final Camera Position
   */
  public static CameraUpdate newLatLngZoom(@NonNull LatLng latLng, double zoom) {
    return new CameraPositionUpdate(-1, latLng, -1, zoom);
  }

  /**
   * Returns a CameraUpdate that shifts the zoom level of the current camera viewpoint.
   *
   * @param amount Amount of zoom level to change with
   * @param focus  Focus point of zoom
   * @return CameraUpdate Final Camera Position
   */
  public static CameraUpdate zoomBy(double amount, Point focus) {
    return new ZoomUpdate(amount, focus.x, focus.y);
  }

  /**
   * Returns a CameraUpdate that shifts the zoom level of the current camera viewpoint.
   *
   * @param amount Amount of zoom level to change with
   * @return CameraUpdate Final Camera Position
   */
  public static CameraUpdate zoomBy(double amount) {
    return new ZoomUpdate(ZoomUpdate.ZOOM_BY, amount);
  }

  /**
   * Returns a CameraUpdate that zooms in on the map by moving the viewpoint's height closer to
   * the Earth's surface. The zoom increment is 1.0.
   *
   * @return CameraUpdate Final Camera Position
   */
  public static CameraUpdate zoomIn() {
    return new ZoomUpdate(ZoomUpdate.ZOOM_IN);
  }

  /**
   * Returns a CameraUpdate that zooms out on the map by moving the viewpoint's height farther
   * away from the Earth's surface. The zoom increment is -1.0.
   *
   * @return CameraUpdate Final Camera Position
   */
  public static CameraUpdate zoomOut() {
    return new ZoomUpdate(ZoomUpdate.ZOOM_OUT);
  }

  /**
   * Returns a CameraUpdate that moves the camera viewpoint to a particular zoom level.
   *
   * @param zoom Zoom level to change to
   * @return CameraUpdate Final Camera Position
   */
  public static CameraUpdate zoomTo(double zoom) {
    return new ZoomUpdate(ZoomUpdate.ZOOM_TO, zoom);
  }

  /**
   * Returns a CameraUpdate that moves the camera viewpoint to a particular bearing.
   *
   * @param bearing Bearing to change to
   * @return CameraUpdate Final Camera Position
   */
  public static CameraUpdate bearingTo(double bearing) {
    return new CameraPositionUpdate(bearing, null, -1, -1);
  }

  /**
   * Returns a CameraUpdate that moves the camera viewpoint to a particular tilt.
   *
   * @param tilt Tilt to change to
   * @return CameraUpdate Final Camera Position
   */
  public static CameraUpdate tiltTo(double tilt) {
    return new CameraPositionUpdate(-1, null, tilt, -1);
  }

  //
  // CameraUpdate types
  //

  static final class CameraPositionUpdate implements CameraUpdate {

    private final double bearing;
    private final LatLng target;
    private final double tilt;
    private final double zoom;

    CameraPositionUpdate(double bearing, LatLng target, double tilt, double zoom) {
      this.bearing = bearing;
      this.target = target;
      this.tilt = tilt;
      this.zoom = zoom;
    }

    public LatLng getTarget() {
      return target;
    }

    public double getBearing() {
      return bearing;
    }

    public double getTilt() {
      return tilt;
    }

    public double getZoom() {
      return zoom;
    }

    @Override
    public CameraPosition getCameraPosition(@NonNull MapboxMap mapboxMap) {
      CameraPosition previousPosition = mapboxMap.getCameraPosition();
      if (target == null) {
        return new CameraPosition.Builder(this)
          .target(previousPosition.target)
          .build();
      }
      return new CameraPosition.Builder(this).build();
    }

    @Override
    public boolean equals(@Nullable Object o) {
      if (this == o) {
        return true;
      }
      if (o == null || getClass() != o.getClass()) {
        return false;
      }

      CameraPositionUpdate that = (CameraPositionUpdate) o;

      if (Double.compare(that.bearing, bearing) != 0) {
        return false;
      }
      if (Double.compare(that.tilt, tilt) != 0) {
        return false;
      }
      if (Double.compare(that.zoom, zoom) != 0) {
        return false;
      }
      return target != null ? target.equals(that.target) : that.target == null;
    }

    @Override
    public int hashCode() {
      int result;
      long temp;
      temp = Double.doubleToLongBits(bearing);
      result = (int) (temp ^ (temp >>> 32));
      result = 31 * result + (target != null ? target.hashCode() : 0);
      temp = Double.doubleToLongBits(tilt);
      result = 31 * result + (int) (temp ^ (temp >>> 32));
      temp = Double.doubleToLongBits(zoom);
      result = 31 * result + (int) (temp ^ (temp >>> 32));
      return result;
    }

    @Override
    public String toString() {
      return "CameraPositionUpdate{"
        + "bearing=" + bearing
        + ", target=" + target
        + ", tilt=" + tilt
        + ", zoom=" + zoom
        + '}';
    }
  }

  static final class CameraBoundsUpdate implements CameraUpdate {

    private LatLngBounds bounds;
    private int[] padding;

    CameraBoundsUpdate(LatLngBounds bounds, int[] padding) {
      this.bounds = bounds;
      this.padding = padding;
    }

    CameraBoundsUpdate(LatLngBounds bounds, int paddingLeft, int paddingTop, int paddingRight, int paddingBottom) {
      this(bounds, new int[] {paddingLeft, paddingTop, paddingRight, paddingBottom});
    }

    public LatLngBounds getBounds() {
      return bounds;
    }

    public int[] getPadding() {
      return padding;
    }

    @Override
    public CameraPosition getCameraPosition(@NonNull MapboxMap mapboxMap) {
      return mapboxMap.getCameraForLatLngBounds(bounds, padding);
    }

    @Override
    public boolean equals(@Nullable Object o) {
      if (this == o) {
        return true;
      }
      if (o == null || getClass() != o.getClass()) {
        return false;
      }

      CameraBoundsUpdate that = (CameraBoundsUpdate) o;

      if (!bounds.equals(that.bounds)) {
        return false;
      }
      return Arrays.equals(padding, that.padding);
    }

    @Override
    public int hashCode() {
      int result = bounds.hashCode();
      result = 31 * result + Arrays.hashCode(padding);
      return result;
    }

    @Override
    public String toString() {
      return "CameraBoundsUpdate{"
        + "bounds=" + bounds
        + ", padding=" + Arrays.toString(padding)
        + '}';
    }
  }

  static final class CameraMoveUpdate implements CameraUpdate {

    private float x;
    private float y;

    CameraMoveUpdate(float x, float y) {
      this.x = x;
      this.y = y;
    }

    @Override
    public CameraPosition getCameraPosition(@NonNull MapboxMap mapboxMap) {
      UiSettings uiSettings = mapboxMap.getUiSettings();
      Projection projection = mapboxMap.getProjection();
      // Calculate the new center point
      float viewPortWidth = uiSettings.getWidth();
      float viewPortHeight = uiSettings.getHeight();
      int[] padding = mapboxMap.getPadding();

      // we inverse the map padding, is reapplied when using moveTo/easeTo or animateTo
      PointF targetPoint = new PointF(
        (viewPortWidth - padding[0] + padding[1]) / 2 + x,
        (viewPortHeight + padding[1] - padding[3]) / 2 + y
      );

      LatLng latLng = projection.fromScreenLocation(targetPoint);
      CameraPosition previousPosition = mapboxMap.getCameraPosition();
      CameraPosition position =
        new CameraPosition.Builder()
          .target(latLng)
          .zoom(previousPosition.zoom)
          .tilt(previousPosition.tilt)
          .bearing(previousPosition.bearing)
          .build();
      return position;
    }

    @Override
    public boolean equals(@Nullable Object o) {
      if (this == o) {
        return true;
      }
      if (o == null || getClass() != o.getClass()) {
        return false;
      }

      CameraMoveUpdate that = (CameraMoveUpdate) o;

      if (Float.compare(that.x, x) != 0) {
        return false;
      }
      return Float.compare(that.y, y) == 0;
    }

    @Override
    public int hashCode() {
      int result = (x != +0.0f ? Float.floatToIntBits(x) : 0);
      result = 31 * result + (y != +0.0f ? Float.floatToIntBits(y) : 0);
      return result;
    }

    @Override
    public String toString() {
      return "CameraMoveUpdate{"
        + "x=" + x
        + ", y=" + y
        + '}';
    }
  }

  static final class ZoomUpdate implements CameraUpdate {

    @IntDef( {ZOOM_IN, ZOOM_OUT, ZOOM_BY, ZOOM_TO, ZOOM_TO_POINT})
    @Retention(RetentionPolicy.SOURCE)
    @interface Type {
    }

    static final int ZOOM_IN = 0;
    static final int ZOOM_OUT = 1;
    static final int ZOOM_BY = 2;
    static final int ZOOM_TO = 3;
    static final int ZOOM_TO_POINT = 4;

    @Type
    private final int type;
    private final double zoom;
    private float x;
    private float y;

    ZoomUpdate(@Type int type) {
      this.type = type;
      this.zoom = 0;
    }

    ZoomUpdate(@Type int type, double zoom) {
      this.type = type;
      this.zoom = zoom;
    }

    ZoomUpdate(double zoom, float x, float y) {
      this.type = ZOOM_TO_POINT;
      this.zoom = zoom;
      this.x = x;
      this.y = y;
    }

    public double getZoom() {
      return zoom;
    }

    @Type
    public int getType() {
      return type;
    }

    public float getX() {
      return x;
    }

    public float getY() {
      return y;
    }

    double transformZoom(double currentZoom) {
      switch (getType()) {
        case CameraUpdateFactory.ZoomUpdate.ZOOM_IN:
          currentZoom++;
          break;
        case CameraUpdateFactory.ZoomUpdate.ZOOM_OUT:
          currentZoom--;
          if (currentZoom < 0) {
            currentZoom = 0;
          }
          break;
        case CameraUpdateFactory.ZoomUpdate.ZOOM_TO:
          currentZoom = getZoom();
          break;
        case CameraUpdateFactory.ZoomUpdate.ZOOM_BY:
          currentZoom = currentZoom + getZoom();
          break;
        case CameraUpdateFactory.ZoomUpdate.ZOOM_TO_POINT:
          currentZoom = currentZoom + getZoom();
          break;
      }
      return currentZoom;
    }

    @Override
    public CameraPosition getCameraPosition(@NonNull MapboxMap mapboxMap) {
      CameraPosition cameraPosition = mapboxMap.getCameraPosition();
      if (getType() != CameraUpdateFactory.ZoomUpdate.ZOOM_TO_POINT) {
        return new CameraPosition.Builder(cameraPosition)
          .zoom(transformZoom(cameraPosition.zoom))
          .build();
      } else {
        return new CameraPosition.Builder(cameraPosition)
          .zoom(transformZoom(cameraPosition.zoom))
          .target(mapboxMap.getProjection().fromScreenLocation(new PointF(getX(), getY())))
          .build();
      }
    }

    @Override
    public boolean equals(@Nullable Object o) {
      if (this == o) {
        return true;
      }
      if (o == null || getClass() != o.getClass()) {
        return false;
      }

      ZoomUpdate that = (ZoomUpdate) o;

      if (type != that.type) {
        return false;
      }
      if (Double.compare(that.zoom, zoom) != 0) {
        return false;
      }
      if (Float.compare(that.x, x) != 0) {
        return false;
      }
      return Float.compare(that.y, y) == 0;
    }

    @Override
    public int hashCode() {
      int result;
      long temp;
      result = type;
      temp = Double.doubleToLongBits(zoom);
      result = 31 * result + (int) (temp ^ (temp >>> 32));
      result = 31 * result + (x != +0.0f ? Float.floatToIntBits(x) : 0);
      result = 31 * result + (y != +0.0f ? Float.floatToIntBits(y) : 0);
      return result;
    }

    @Override
    public String toString() {
      return "ZoomUpdate{"
        + "type=" + type
        + ", zoom=" + zoom
        + ", x=" + x
        + ", y=" + y
        + '}';
    }
  }
}