forked from mapbox/mapbox-gl-native-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapboxMap.java
More file actions
2501 lines (2249 loc) · 85 KB
/
MapboxMap.java
File metadata and controls
2501 lines (2249 loc) · 85 KB
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.mapbox.mapboxsdk.maps;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.PointF;
import android.graphics.RectF;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import com.mapbox.android.gestures.AndroidGesturesManager;
import com.mapbox.android.gestures.MoveGestureDetector;
import com.mapbox.android.gestures.RotateGestureDetector;
import com.mapbox.android.gestures.ShoveGestureDetector;
import com.mapbox.android.gestures.StandardScaleGestureDetector;
import com.mapbox.geojson.Feature;
import com.mapbox.geojson.Geometry;
import com.mapbox.mapboxsdk.MapStrictMode;
import com.mapbox.mapboxsdk.annotations.Annotation;
import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions;
import com.mapbox.mapboxsdk.annotations.Marker;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.annotations.Polygon;
import com.mapbox.mapboxsdk.annotations.PolygonOptions;
import com.mapbox.mapboxsdk.annotations.Polyline;
import com.mapbox.mapboxsdk.annotations.PolylineOptions;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.camera.CameraUpdate;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.location.LocationComponent;
import com.mapbox.mapboxsdk.log.Logger;
import com.mapbox.mapboxsdk.offline.OfflineRegionDefinition;
import com.mapbox.mapboxsdk.style.expressions.Expression;
import java.util.ArrayList;
import java.util.List;
import androidx.annotation.FloatRange;
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.Size;
import androidx.annotation.UiThread;
/**
* The general class to interact with in the Android Mapbox SDK. It exposes the entry point for all
* methods related to the MapView. You cannot instantiate {@link MapboxMap} object directly, rather,
* you must obtain one from the getMapAsync() method on a MapFragment or MapView that you have
* added to your application.
* <p>
* Note: Similar to a View object, a MapboxMap should only be read and modified from the main thread.
* </p>
*/
@UiThread
public final class MapboxMap {
private static final String TAG = "Mbgl-MapboxMap";
private final NativeMap nativeMapView;
private final UiSettings uiSettings;
private final Projection projection;
private final Transform transform;
private final CameraChangeDispatcher cameraChangeDispatcher;
private final OnGesturesManagerInteractionListener onGesturesManagerInteractionListener;
private final List<Style.OnStyleLoaded> awaitingStyleGetters = new ArrayList<>();
private final List<OnDeveloperAnimationListener> developerAnimationStartedListeners;
@Nullable
private Style.OnStyleLoaded styleLoadedCallback;
private LocationComponent locationComponent;
private AnnotationManager annotationManager;
@Nullable
private MapboxMap.OnFpsChangedListener onFpsChangedListener;
@Nullable
private Style style;
private boolean debugActive;
private boolean started;
MapboxMap(NativeMap map, Transform transform, UiSettings ui, Projection projection,
OnGesturesManagerInteractionListener listener, CameraChangeDispatcher cameraChangeDispatcher,
List<OnDeveloperAnimationListener> developerAnimationStartedListeners) {
this.nativeMapView = map;
this.uiSettings = ui;
this.projection = projection;
this.transform = transform;
this.onGesturesManagerInteractionListener = listener;
this.cameraChangeDispatcher = cameraChangeDispatcher;
this.developerAnimationStartedListeners = developerAnimationStartedListeners;
}
/**
* Trigger the mapview to repaint.
*/
public void triggerRepaint() {
nativeMapView.triggerRepaint();
}
void initialise(@NonNull Context context, @NonNull MapboxMapOptions options) {
transform.initialise(this, options);
uiSettings.initialise(context, options);
// Map configuration
setDebugActive(options.getDebugActive());
setApiBaseUrl(options);
setPrefetchesTiles(options);
}
/**
* Get the Style of the map asynchronously.
*/
public void getStyle(@NonNull Style.OnStyleLoaded onStyleLoaded) {
if (style != null && style.isFullyLoaded()) {
onStyleLoaded.onStyleLoaded(style);
} else {
awaitingStyleGetters.add(onStyleLoaded);
}
}
/**
* Get the Style of the map.
* <p>
* Returns null when style is being loaded.
* </p>
*
* @return the style of the map
*/
@Nullable
public Style getStyle() {
if (style == null || !style.isFullyLoaded()) {
return null;
} else {
return style;
}
}
/**
* Called when the hosting Activity/Fragment onStart() method is called.
*/
void onStart() {
started = true;
locationComponent.onStart();
}
/**
* Called when the hosting Activity/Fragment onStop() method is called.
*/
void onStop() {
started = false;
locationComponent.onStop();
}
/**
* Called when the hosting Activity/Fragment is going to be destroyed and map state needs to be saved.
*
* @param outState the bundle to save the state to.
*/
void onSaveInstanceState(@NonNull Bundle outState) {
outState.putParcelable(MapboxConstants.STATE_CAMERA_POSITION, transform.getCameraPosition());
outState.putBoolean(MapboxConstants.STATE_DEBUG_ACTIVE, isDebugActive());
uiSettings.onSaveInstanceState(outState);
}
/**
* Called when the hosting Activity/Fragment is recreated and map state needs to be restored.
*
* @param savedInstanceState the bundle containing the saved state
*/
void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
final CameraPosition cameraPosition = savedInstanceState.getParcelable(MapboxConstants.STATE_CAMERA_POSITION);
uiSettings.onRestoreInstanceState(savedInstanceState);
if (cameraPosition != null) {
moveCamera(CameraUpdateFactory.newCameraPosition(
new CameraPosition.Builder(cameraPosition).build())
);
}
nativeMapView.setDebug(savedInstanceState.getBoolean(MapboxConstants.STATE_DEBUG_ACTIVE));
}
/**
* Called when the hosting Activity/Fragment onDestroy()/onDestroyView() method is called.
*/
void onDestroy() {
locationComponent.onDestroy();
if (style != null) {
style.clear();
}
cameraChangeDispatcher.onDestroy();
}
/**
* Called before the OnMapReadyCallback is invoked.
*/
void onPreMapReady() {
transform.invalidateCameraPosition();
annotationManager.reloadMarkers();
annotationManager.adjustTopOffsetPixels(this);
}
/**
* Called when the OnMapReadyCallback has finished executing.
* <p>
* Invalidation of the camera position is required to update the added components in
* OnMapReadyCallback with the correct transformation.
* </p>
*/
void onPostMapReady() {
transform.invalidateCameraPosition();
}
/**
* Called when the map finished loading a style.
*/
void onFinishLoadingStyle() {
notifyStyleLoaded();
}
/**
* Called when the map failed loading a style.
*/
void onFailLoadingStyle() {
styleLoadedCallback = null;
}
/**
* Called when the region is changing or has changed.
*/
void onUpdateRegionChange() {
annotationManager.update();
}
/**
* Called when the map frame is fully rendered.
*/
void onUpdateFullyRendered() {
CameraPosition cameraPosition = transform.invalidateCameraPosition();
if (cameraPosition != null) {
uiSettings.update(cameraPosition);
}
}
/**
* Experimental feature. Do not use.
*/
long getNativeMapPtr() {
return nativeMapView.getNativePtr();
}
// Style
/**
* Sets tile pre-fetching zoom delta from MapboxOptions.
*
* @param options the options object
*/
private void setPrefetchesTiles(@NonNull MapboxMapOptions options) {
if (!options.getPrefetchesTiles()) {
setPrefetchZoomDelta(0);
} else {
setPrefetchZoomDelta(options.getPrefetchZoomDelta());
}
}
/**
* Enable or disable tile pre-fetching. Pre-fetching makes sure that a low-resolution
* tile is rendered as soon as possible at the expense of a little bandwidth.
*
* @param enable true to enable
* @deprecated Use {@link #setPrefetchZoomDelta(int)} instead.
*/
@Deprecated
public void setPrefetchesTiles(boolean enable) {
nativeMapView.setPrefetchTiles(enable);
}
/**
* Check whether tile pre-fetching is enabled or not.
*
* @return true if enabled
* @see MapboxMap#setPrefetchesTiles(boolean)
* @deprecated Use {@link #getPrefetchZoomDelta()} instead.
*/
@Deprecated
public boolean getPrefetchesTiles() {
return nativeMapView.getPrefetchTiles();
}
/**
* Set the tile pre-fetching zoom delta. Pre-fetching makes sure that a low-resolution
* tile at the (current_zoom_level - delta) is rendered as soon as possible at the
* expense of a little bandwidth.
* Note: This operation will override the MapboxMapOptions#setPrefetchesTiles(boolean)
* Setting zoom delta to 0 will disable pre-fetching.
* Default zoom delta is 4.
*
* @param delta zoom delta
*/
public void setPrefetchZoomDelta(@IntRange(from = 0) int delta) {
nativeMapView.setPrefetchZoomDelta(delta);
}
/**
* Check current pre-fetching zoom delta.
*
* @return current zoom delta.
* @see MapboxMap#setPrefetchZoomDelta(int)
*/
@IntRange(from = 0)
public int getPrefetchZoomDelta() {
return nativeMapView.getPrefetchZoomDelta();
}
//
// MinZoom
//
/**
* <p>
* Sets the minimum zoom level the map can be displayed at.
* </p>
*
* @param minZoom The new minimum zoom level.
*/
public void setMinZoomPreference(
@FloatRange(from = MapboxConstants.MINIMUM_ZOOM, to = MapboxConstants.MAXIMUM_ZOOM) double minZoom) {
transform.setMinZoom(minZoom);
}
/**
* <p>
* Gets the minimum zoom level the map can be displayed at.
* </p>
*
* @return The minimum zoom level.
*/
public double getMinZoomLevel() {
return transform.getMinZoom();
}
//
// MaxZoom
//
/**
* <p>
* Sets the maximum zoom level the map can be displayed at.
* </p>
* <p>
* The default maximum zoomn level is 22. The upper bound for this value is 25.5.
* </p>
*
* @param maxZoom The new maximum zoom level.
*/
public void setMaxZoomPreference(@FloatRange(from = MapboxConstants.MINIMUM_ZOOM,
to = MapboxConstants.MAXIMUM_ZOOM) double maxZoom) {
transform.setMaxZoom(maxZoom);
}
/**
* <p>
* Gets the maximum zoom level the map can be displayed at.
* </p>
*
* @return The maximum zoom level.
*/
public double getMaxZoomLevel() {
return transform.getMaxZoom();
}
//
// MinPitch
//
/**
* <p>
* Sets the minimum Pitch the map can be displayed at.
* </p>
*
* <p>
* The default and lower bound for minPitch Pitch is 0.
* </p>
* @param minPitch The new minimum Pitch.
*/
public void setMinPitchPreference(
@FloatRange(from = MapboxConstants.MINIMUM_PITCH, to = MapboxConstants.MAXIMUM_PITCH) double minPitch) {
transform.setMinPitch(minPitch);
}
/**
* <p>
* Gets the minimum Pitch the map can be displayed at.
* </p>
*
* @return The minimum Pitch.
*/
public double getMinPitch() {
return transform.getMinPitch();
}
//
// MaxPitch
//
/**
* <p>
* Sets the maximum Pitch the map can be displayed at.
* </p>
* <p>
* The default and upper bound for maximum Pitch is 60.
* </p>
*
* @param maxPitch The new maximum Pitch.
*/
public void setMaxPitchPreference(@FloatRange(from = MapboxConstants.MINIMUM_PITCH,
to = MapboxConstants.MAXIMUM_PITCH) double maxPitch) {
transform.setMaxPitch(maxPitch);
}
/**
* <p>
* Gets the maximum Pitch the map can be displayed at.
* </p>
*
* @return The maximum Pitch.
*/
public double getMaxPitch() {
return transform.getMaxPitch();
}
//
// UiSettings
//
/**
* Gets the user interface settings for the map.
*
* @return the UiSettings associated with this map
*/
@NonNull
public UiSettings getUiSettings() {
return uiSettings;
}
//
// Projection
//
/**
* Get the Projection object that you can use to convert between screen coordinates and latitude/longitude
* coordinates.
*
* @return the Projection associated with this map
*/
@NonNull
public Projection getProjection() {
return projection;
}
//
// Camera API
//
/**
* Cancels ongoing animations.
* <p>
* This invokes the {@link CancelableCallback} for ongoing camera updates.
* </p>
*/
public void cancelTransitions() {
transform.cancelTransitions();
}
/**
* Gets the current position of the camera.
* The CameraPosition returned is a snapshot of the current position, and will not automatically update when the
* camera moves.
*
* @return The current position of the Camera.
*/
@NonNull
public final CameraPosition getCameraPosition() {
return transform.getCameraPosition();
}
/**
* Repositions the camera according to the cameraPosition.
* The move is instantaneous, and a subsequent getCameraPosition() will reflect the new position.
* See CameraUpdateFactory for a set of updates.
*
* @param cameraPosition the camera position to set
*/
public void setCameraPosition(@NonNull CameraPosition cameraPosition) {
moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition), null);
}
/**
* Repositions the camera according to the instructions defined in the update.
* The move is instantaneous, and a subsequent getCameraPosition() will reflect the new position.
* See CameraUpdateFactory for a set of updates.
*
* @param update The change that should be applied to the camera.
*/
public final void moveCamera(@NonNull CameraUpdate update) {
moveCamera(update, null);
}
/**
* Repositions the camera according to the instructions defined in the update.
* The move is instantaneous, and a subsequent getCameraPosition() will reflect the new position.
* See CameraUpdateFactory for a set of updates.
*
* @param update The change that should be applied to the camera
* @param callback the callback to be invoked when an animation finishes or is canceled
*/
public final void moveCamera(@NonNull final CameraUpdate update,
@Nullable final MapboxMap.CancelableCallback callback) {
notifyDeveloperAnimationListeners();
transform.moveCamera(MapboxMap.this, update, callback);
}
/**
* Gradually move the camera by the default duration, zoom will not be affected unless specified
* within {@link CameraUpdate}. If {@link #getCameraPosition()} is called during the animation,
* it will return the current location of the camera in flight.
*
* @param update The change that should be applied to the camera.
* @see com.mapbox.mapboxsdk.camera.CameraUpdateFactory for a set of updates.
*/
public final void easeCamera(@NonNull CameraUpdate update) {
easeCamera(update, MapboxConstants.ANIMATION_DURATION);
}
/**
* Gradually move the camera by the default duration, zoom will not be affected unless specified
* within {@link CameraUpdate}. If {@link #getCameraPosition()} is called during the animation,
* it will return the current location of the camera in flight.
*
* @param update The change that should be applied to the camera.
* @param callback An optional callback to be notified from the main thread when the animation
* stops. If the animation stops due to its natural completion, the callback
* will be notified with onFinish(). If the animation stops due to interruption
* by a later camera movement or a user gesture, onCancel() will be called.
* Do not update or ease the camera from within onCancel().
* @see com.mapbox.mapboxsdk.camera.CameraUpdateFactory for a set of updates.
*/
public final void easeCamera(@NonNull CameraUpdate update, @Nullable final MapboxMap.CancelableCallback callback) {
easeCamera(update, MapboxConstants.ANIMATION_DURATION, callback);
}
/**
* Gradually move the camera by a specified duration in milliseconds, zoom will not be affected
* unless specified within {@link CameraUpdate}. If {@link #getCameraPosition()} is called
* during the animation, it will return the current location of the camera in flight.
*
* @param update The change that should be applied to the camera.
* @param durationMs The duration of the animation in milliseconds. This must be strictly
* positive, otherwise an IllegalArgumentException will be thrown.
* @see com.mapbox.mapboxsdk.camera.CameraUpdateFactory for a set of updates.
*/
public final void easeCamera(@NonNull CameraUpdate update, int durationMs) {
easeCamera(update, durationMs, null);
}
/**
* Gradually move the camera by a specified duration in milliseconds, zoom will not be affected
* unless specified within {@link CameraUpdate}. A callback can be used to be notified when
* easing the camera stops. If {@link #getCameraPosition()} is called during the animation, it
* will return the current location of the camera in flight.
* <p>
* Note that this will cancel location tracking mode if enabled.
* </p>
*
* @param update The change that should be applied to the camera.
* @param durationMs The duration of the animation in milliseconds. This must be strictly
* positive, otherwise an IllegalArgumentException will be thrown.
* @param callback An optional callback to be notified from the main thread when the animation
* stops. If the animation stops due to its natural completion, the callback
* will be notified with onFinish(). If the animation stops due to interruption
* by a later camera movement or a user gesture, onCancel() will be called.
* Do not update or ease the camera from within onCancel().
* @see com.mapbox.mapboxsdk.camera.CameraUpdateFactory for a set of updates.
*/
public final void easeCamera(@NonNull CameraUpdate update, int durationMs,
@Nullable final MapboxMap.CancelableCallback callback) {
easeCamera(update, durationMs, true, callback);
}
/**
* Gradually move the camera by a specified duration in milliseconds, zoom will not be affected
* unless specified within {@link CameraUpdate}. A callback can be used to be notified when
* easing the camera stops. If {@link #getCameraPosition()} is called during the animation, it
* will return the current location of the camera in flight.
* <p>
* Note that this will cancel location tracking mode if enabled.
* </p>
*
* @param update The change that should be applied to the camera.
* @param durationMs The duration of the animation in milliseconds. This must be strictly
* positive, otherwise an IllegalArgumentException will be thrown.
* @param easingInterpolator True for easing interpolator, false for linear.
*/
public final void easeCamera(@NonNull CameraUpdate update, int durationMs, boolean easingInterpolator) {
easeCamera(update, durationMs, easingInterpolator, null);
}
/**
* Gradually move the camera by a specified duration in milliseconds, zoom will not be affected
* unless specified within {@link CameraUpdate}. A callback can be used to be notified when
* easing the camera stops. If {@link #getCameraPosition()} is called during the animation, it
* will return the current location of the camera in flight.
*
* @param update The change that should be applied to the camera.
* @param durationMs The duration of the animation in milliseconds. This must be strictly
* positive, otherwise an IllegalArgumentException will be thrown.
* @param easingInterpolator True for easing interpolator, false for linear.
* @param callback An optional callback to be notified from the main thread when the animation
* stops. If the animation stops due to its natural completion, the callback
* will be notified with onFinish(). If the animation stops due to interruption
* by a later camera movement or a user gesture, onCancel() will be called.
* Do not update or ease the camera from within onCancel().
*/
public final void easeCamera(@NonNull final CameraUpdate update,
final int durationMs,
final boolean easingInterpolator,
@Nullable final MapboxMap.CancelableCallback callback) {
if (durationMs <= 0) {
throw new IllegalArgumentException("Null duration passed into easeCamera");
}
notifyDeveloperAnimationListeners();
transform.easeCamera(MapboxMap.this, update, durationMs, easingInterpolator, callback);
}
/**
* Animate the camera to a new location defined within {@link CameraUpdate} using a transition
* animation that evokes powered flight. The animation will last the default amount of time.
* During the animation, a call to {@link #getCameraPosition()} returns an intermediate location
* of the camera in flight.
*
* @param update The change that should be applied to the camera.
* @see com.mapbox.mapboxsdk.camera.CameraUpdateFactory for a set of updates.
*/
public final void animateCamera(@NonNull CameraUpdate update) {
animateCamera(update, MapboxConstants.ANIMATION_DURATION, null);
}
/**
* Animate the camera to a new location defined within {@link CameraUpdate} using a transition
* animation that evokes powered flight. The animation will last the default amount of time. A
* callback can be used to be notified when animating the camera stops. During the animation, a
* call to {@link #getCameraPosition()} returns an intermediate location of the camera in flight.
*
* @param update The change that should be applied to the camera.
* @param callback The callback to invoke from the main thread when the animation stops. If the
* animation completes normally, onFinish() is called; otherwise, onCancel() is
* called. Do not update or animate the camera from within onCancel().
* @see com.mapbox.mapboxsdk.camera.CameraUpdateFactory for a set of updates.
*/
public final void animateCamera(@NonNull CameraUpdate update, @Nullable MapboxMap.CancelableCallback callback) {
animateCamera(update, MapboxConstants.ANIMATION_DURATION, callback);
}
/**
* Animate the camera to a new location defined within {@link CameraUpdate} using a transition
* animation that evokes powered flight. The animation will last a specified amount of time
* given in milliseconds. During the animation, a call to {@link #getCameraPosition()} returns
* an intermediate location of the camera in flight.
*
* @param update The change that should be applied to the camera.
* @param durationMs The duration of the animation in milliseconds. This must be strictly
* positive, otherwise an IllegalArgumentException will be thrown.
* @see com.mapbox.mapboxsdk.camera.CameraUpdateFactory for a set of updates.
*/
public final void animateCamera(@NonNull CameraUpdate update, int durationMs) {
animateCamera(update, durationMs, null);
}
/**
* Animate the camera to a new location defined within {@link CameraUpdate} using a transition
* animation that evokes powered flight. The animation will last a specified amount of time
* given in milliseconds. A callback can be used to be notified when animating the camera stops.
* During the animation, a call to {@link #getCameraPosition()} returns an intermediate location
* of the camera in flight.
*
* @param update The change that should be applied to the camera.
* @param durationMs The duration of the animation in milliseconds. This must be strictly
* positive, otherwise an IllegalArgumentException will be thrown.
* @param callback An optional callback to be notified from the main thread when the animation
* stops. If the animation stops due to its natural completion, the callback
* will be notified with onFinish(). If the animation stops due to interruption
* by a later camera movement or a user gesture, onCancel() will be called.
* Do not update or animate the camera from within onCancel(). If a callback
* isn't required, leave it as null.
* @see com.mapbox.mapboxsdk.camera.CameraUpdateFactory for a set of updates.
*/
public final void animateCamera(@NonNull final CameraUpdate update, final int durationMs,
@Nullable final MapboxMap.CancelableCallback callback) {
if (durationMs <= 0) {
throw new IllegalArgumentException("Null duration passed into animateCamera");
}
notifyDeveloperAnimationListeners();
transform.animateCamera(MapboxMap.this, update, durationMs, callback);
}
/**
* Scrolls the camera over the map, shifting the center of view by the specified number of pixels in the x and y
* directions.
*
* @param x Amount of pixels to scroll to in x direction
* @param y Amount of pixels to scroll to in y direction
*/
public void scrollBy(float x, float y) {
scrollBy(x, y, 0);
}
/**
* Scrolls the camera over the map, shifting the center of view by the specified number of pixels in the x and y
* directions.
*
* @param x Amount of pixels to scroll to in x direction
* @param y Amount of pixels to scroll to in y direction
* @param duration Amount of time the scrolling should take
*/
public void scrollBy(float x, float y, long duration) {
notifyDeveloperAnimationListeners();
nativeMapView.moveBy(x, y, duration);
}
//
// Reset North
//
/**
* Resets the map view to face north.
*/
public void resetNorth() {
notifyDeveloperAnimationListeners();
transform.resetNorth();
}
/**
* Transform the map bearing given a bearing, focal point coordinates, and a duration.
*
* @param bearing The bearing of the Map to be transformed to
* @param focalX The x coordinate of the focal point
* @param focalY The y coordinate of the focal point
* @param duration The duration of the transformation
*/
public void setFocalBearing(double bearing, float focalX, float focalY, long duration) {
notifyDeveloperAnimationListeners();
transform.setBearing(bearing, focalX, focalY, duration);
}
/**
* Returns the measured height of the Map.
*
* @return the height of the map
*/
public float getHeight() {
return projection.getHeight();
}
/**
* Returns the measured width of the Map.
*
* @return the width of the map
*/
public float getWidth() {
return projection.getWidth();
}
//
// Offline
//
/**
* Loads a new style from the specified offline region definition and moves the map camera to that region.
*
* @param definition the offline region definition
* @see OfflineRegionDefinition
*/
public void setOfflineRegionDefinition(@NonNull OfflineRegionDefinition definition) {
setOfflineRegionDefinition(definition, null);
}
/**
* Loads a new style from the specified offline region definition and moves the map camera to that region.
*
* @param definition the offline region definition
* @param callback the callback to be invoked when the style has loaded
* @see OfflineRegionDefinition
*/
public void setOfflineRegionDefinition(@NonNull OfflineRegionDefinition definition,
@Nullable Style.OnStyleLoaded callback) {
double minZoom = definition.getMinZoom();
double maxZoom = definition.getMaxZoom();
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(definition.getBounds().getCenter())
.zoom(minZoom)
.build();
moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
setMinZoomPreference(minZoom);
setMaxZoomPreference(maxZoom);
setStyle(new Style.Builder().fromUri(definition.getStyleURL()), callback);
}
//
// Debug
//
/**
* Returns whether the map debug information is currently shown.
*
* @return If true, map debug information is currently shown.
*/
public boolean isDebugActive() {
return debugActive;
}
/**
* <p>
* Changes whether the map debug information is shown.
* </p>
* The default value is false.
*
* @param debugActive If true, map debug information is shown.
*/
public void setDebugActive(boolean debugActive) {
this.debugActive = debugActive;
nativeMapView.setDebug(debugActive);
}
/**
* <p>
* Cycles through the map debug options.
* </p>
* The value of isDebugActive reflects whether there are
* any map debug options enabled or disabled.
*
* @see #isDebugActive()
* @deprecated use {@link #setDebugActive(boolean)}
*/
@Deprecated
public void cycleDebugOptions() {
this.debugActive = !nativeMapView.getDebug();
nativeMapView.setDebug(debugActive);
}
//
// API endpoint config
//
private void setApiBaseUrl(@NonNull MapboxMapOptions options) {
String apiBaseUrl = options.getApiBaseUrl();
if (!TextUtils.isEmpty(apiBaseUrl)) {
nativeMapView.setApiBaseUrl(apiBaseUrl);
}
}
//
// Styling
//
/**
* Loads a new map style from the specified bundled style.
* <p>
* This method is asynchronous and will return before the style finishes loading.
* If you wish to wait for the map to finish loading, listen to the {@link MapView.OnDidFinishLoadingStyleListener}
* callback or use the {@link #setStyle(String, Style.OnStyleLoaded)} method instead.
* </p>
* If the style fails to load or an invalid style URL is set, the map view will become blank.
* An error message will be logged in the Android logcat and {@link MapView.OnDidFailLoadingMapListener} callback
* will be triggered.
*
* @param style The bundled style
* @see Style
*/
public void setStyle(@Style.StyleUrl String style) {
this.setStyle(style, null);
}
/**
* Loads a new map style from the specified bundled style.
* <p>
* If the style fails to load or an invalid style URL is set, the map view will become blank.
* An error message will be logged in the Android logcat and {@link MapView.OnDidFailLoadingMapListener} callback
* will be triggered.
* </p>
*
* @param style The bundled style
* @param callback The callback to be invoked when the style has loaded
* @see Style
*/
public void setStyle(@Style.StyleUrl String style, final Style.OnStyleLoaded callback) {
this.setStyle(new Style.Builder().fromUri(style), callback);
}
/**
* Loads a new map style from the specified builder.
* <p>
* If the builder fails to load, the map view will become blank. An error message will be logged in the Android logcat
* and {@link MapView.OnDidFailLoadingMapListener} callback will be triggered. If you wish to wait for the map to
* finish loading, listen to the {@link MapView.OnDidFinishLoadingStyleListener} callback or use the
* {@link #setStyle(String, Style.OnStyleLoaded)} instead.
* </p>
*
* @param builder The style builder
* @see Style
*/
public void setStyle(Style.Builder builder) {
this.setStyle(builder, null);
}
/**
* Loads a new map style from the specified builder.
* <p>
* If the builder fails to load, the map view will become blank. An error message will be logged in the Android logcat
* and {@link MapView.OnDidFailLoadingMapListener} callback will be triggered.
* </p>
*
* @param builder The style builder
* @param callback The callback to be invoked when the style has loaded
* @see Style
*/
public void setStyle(Style.Builder builder, final Style.OnStyleLoaded callback) {
styleLoadedCallback = callback;
locationComponent.onStartLoadingMap();
if (style != null) {
style.clear();
}
style = builder.build(nativeMapView);
if (!TextUtils.isEmpty(builder.getUri())) {
nativeMapView.setStyleUri(builder.getUri());
} else if (!TextUtils.isEmpty(builder.getJson())) {
nativeMapView.setStyleJson(builder.getJson());
} else {
// user didn't provide a `from` component, load a blank style instead
nativeMapView.setStyleJson(Style.EMPTY_JSON);
}
}
void notifyStyleLoaded() {
if (nativeMapView.isDestroyed()) {
return;
}
if (style != null) {
style.onDidFinishLoadingStyle();
locationComponent.onFinishLoadingStyle();
// notify the listener provided with the style setter
if (styleLoadedCallback != null) {
styleLoadedCallback.onStyleLoaded(style);
}
// notify style getters
for (Style.OnStyleLoaded styleGetter : awaitingStyleGetters) {
styleGetter.onStyleLoaded(style);
}
} else {
MapStrictMode.strictModeViolation("No style to provide.");
}
styleLoadedCallback = null;
awaitingStyleGetters.clear();
}
//
// Annotations
//
/**
* <p>
* Adds a marker to this map.
* </p>
* The marker's icon is rendered on the map at the location {@code Marker.position}.
* If {@code Marker.title} is defined, the map shows an info box with the marker's title and snippet.
*
* @param markerOptions A marker options object that defines how to render the marker
* @return The {@code Marker} that was added to the map
* @deprecated As of 7.0.0,
* use <a href="https://github.com/mapbox/mapbox-plugins-android/tree/master/plugin-annotation">
* Mapbox Annotation Plugin</a> instead
*/
@Deprecated
@NonNull
public Marker addMarker(@NonNull MarkerOptions markerOptions) {
return annotationManager.addMarker(markerOptions, this);
}
/**
* <p>