summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMapOptions.java
blob: 941d2f1c60797d5ac5d18e3df9bfc79a9e7ef9e7 (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
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
package com.mapbox.mapboxsdk.maps;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;

import com.mapbox.mapboxsdk.R;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.constants.MapboxConstants;

/**
 * Defines configuration MapboxMapMapOptions for a MapboxMap. These options can be used when adding a
 * map to your application programmatically (as opposed to via XML). If you are using a MapFragment,
 * you can pass these options in using the static factory method newInstance(MapboxMapOptions).
 * If you are using a MapView, you can pass these options in using the constructor
 * MapView(Context, MapboxMapOptions). If you add a map using XML, then you can apply these options
 * using custom XML tags.
 */
public class MapboxMapOptions implements Parcelable {

    private static final float DIMENSION_SEVEN_DP = 7f;
    private static final float DIMENSION_TEN_DP = 10f;
    private static final float DIMENSION_SIXTEEN_DP = 16f;
    private static final float DIMENSION_SEVENTY_SIX_DP = 76f;

    private CameraPosition cameraPosition;

    private boolean debugActive;

    private boolean compassEnabled = true;
    private int compassGravity = Gravity.TOP | Gravity.END;
    private int compassMargins[];

    private boolean logoEnabled = true;
    private int logoGravity = Gravity.BOTTOM | Gravity.START;
    private int logoMargins[];

    private boolean attributionEnabled = true;
    private int attributionGravity = Gravity.BOTTOM;
    private int attributionMargins[];

    private float minZoom = MapboxConstants.MINIMUM_ZOOM;
    private float maxZoom = MapboxConstants.MAXIMUM_ZOOM;

    private boolean rotateGesturesEnabled = true;
    private boolean scrollGesturesEnabled = true;
    private boolean tiltGesturesEnabled = true;
    private boolean zoomGesturesEnabled = true;
    private boolean zoomControlsEnabled = false;

    private boolean locationEnabled;

    private String style;
    private String accessToken;

    /**
     * Creates a new MapboxMapOptions object.
     */
    public MapboxMapOptions() {
    }

    private MapboxMapOptions(Parcel in) {
        cameraPosition = in.readParcelable(CameraPosition.class.getClassLoader());
        debugActive = in.readByte() != 0;

        compassEnabled = in.readByte() != 0;
        compassGravity = in.readInt();
        compassMargins = in.createIntArray();

        logoEnabled = in.readByte() != 0;
        logoGravity = in.readInt();
        logoMargins = in.createIntArray();

        attributionEnabled = in.readByte() != 0;
        attributionGravity = in.readInt();
        attributionMargins = in.createIntArray();

        minZoom = in.readFloat();
        maxZoom = in.readFloat();

        rotateGesturesEnabled = in.readByte() != 0;
        scrollGesturesEnabled = in.readByte() != 0;
        tiltGesturesEnabled = in.readByte() != 0;
        zoomControlsEnabled = in.readByte() != 0;
        zoomGesturesEnabled = in.readByte() != 0;

        locationEnabled = in.readByte() != 0;

        style = in.readString();
        accessToken = in.readString();
    }

    /**
     * Creates a GoogleMapsOptions from the attribute set
     *
     * @param context Context related to a map view.
     * @param attrs   Attributeset containing configuration
     * @return
     */
    public static MapboxMapOptions createFromAttributes(@NonNull Context context, @Nullable AttributeSet attrs) {
        MapboxMapOptions mapboxMapOptions = new MapboxMapOptions();
        float screenDensity = context.getResources().getDisplayMetrics().density;
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MapView, 0, 0);
        try {
            mapboxMapOptions.debugActive(typedArray.getBoolean(R.styleable.MapView_debug_active, false));

            mapboxMapOptions.camera(new CameraPosition.Builder(typedArray).build());

            mapboxMapOptions.accessToken(typedArray.getString(R.styleable.MapView_access_token));
            mapboxMapOptions.styleUrl(typedArray.getString(R.styleable.MapView_style_url));

            mapboxMapOptions.zoomGesturesEnabled(typedArray.getBoolean(R.styleable.MapView_zoom_enabled, true));
            mapboxMapOptions.scrollGesturesEnabled(typedArray.getBoolean(R.styleable.MapView_scroll_enabled, true));
            mapboxMapOptions.rotateGesturesEnabled(typedArray.getBoolean(R.styleable.MapView_rotate_enabled, true));
            mapboxMapOptions.tiltGesturesEnabled(typedArray.getBoolean(R.styleable.MapView_tilt_enabled, true));
            mapboxMapOptions.zoomControlsEnabled(typedArray.getBoolean(R.styleable.MapView_zoom_controls_enabled, false));

            mapboxMapOptions.maxZoom(typedArray.getFloat(R.styleable.MapView_zoom_max, MapboxConstants.MAXIMUM_ZOOM));
            mapboxMapOptions.minZoom(typedArray.getFloat(R.styleable.MapView_zoom_min, MapboxConstants.MINIMUM_ZOOM));

            mapboxMapOptions.compassEnabled(typedArray.getBoolean(R.styleable.MapView_compass_enabled, true));
            mapboxMapOptions.compassGravity(typedArray.getInt(R.styleable.MapView_compass_gravity, Gravity.TOP | Gravity.END));
            mapboxMapOptions.compassMargins(new int[]{(int) (typedArray.getDimension(R.styleable.MapView_compass_margin_left, DIMENSION_TEN_DP) * screenDensity)
                    , ((int) typedArray.getDimension(R.styleable.MapView_compass_margin_top, DIMENSION_TEN_DP * screenDensity))
                    , ((int) typedArray.getDimension(R.styleable.MapView_compass_margin_right, DIMENSION_TEN_DP * screenDensity))
                    , ((int) typedArray.getDimension(R.styleable.MapView_compass_margin_bottom, DIMENSION_TEN_DP * screenDensity))});

            mapboxMapOptions.logoEnabled(typedArray.getBoolean(R.styleable.MapView_logo_visibility, true));
            mapboxMapOptions.logoGravity(typedArray.getInt(R.styleable.MapView_logo_gravity, Gravity.BOTTOM | Gravity.START));
            mapboxMapOptions.logoMargins(new int[]{(int) (typedArray.getDimension(R.styleable.MapView_logo_margin_left, DIMENSION_SIXTEEN_DP) * screenDensity)
                    , (int) (typedArray.getDimension(R.styleable.MapView_logo_margin_top, DIMENSION_SIXTEEN_DP) * screenDensity)
                    , (int) (typedArray.getDimension(R.styleable.MapView_logo_margin_right, DIMENSION_SIXTEEN_DP) * screenDensity)
                    , (int) (typedArray.getDimension(R.styleable.MapView_logo_margin_bottom, DIMENSION_SIXTEEN_DP) * screenDensity)});

            mapboxMapOptions.attributionEnabled(typedArray.getBoolean(R.styleable.MapView_attribution_visibility, true));
            mapboxMapOptions.attributionGravity(typedArray.getInt(R.styleable.MapView_attribution_gravity, Gravity.BOTTOM));
            mapboxMapOptions.attributionMargins(new int[]{(int) (typedArray.getDimension(R.styleable.MapView_attribution_margin_left, DIMENSION_SEVENTY_SIX_DP) * screenDensity)
                    , (int) (typedArray.getDimension(R.styleable.MapView_attribution_margin_top, DIMENSION_SEVEN_DP) * screenDensity)
                    , (int) (typedArray.getDimension(R.styleable.MapView_attribution_margin_right, DIMENSION_SEVEN_DP) * screenDensity)
                    , (int) (typedArray.getDimension(R.styleable.MapView_attribution_margin_bottom, DIMENSION_SEVEN_DP) * screenDensity)});

            mapboxMapOptions.locationEnabled(typedArray.getBoolean(R.styleable.MapView_my_location_enabled, false));
        } finally {
            typedArray.recycle();
        }
        return mapboxMapOptions;
    }

    /**
     * Specifies a the initial camera position for the map view.
     *
     * @param cameraPosition Inital camera position
     * @return This
     */
    public MapboxMapOptions camera(CameraPosition cameraPosition) {
        this.cameraPosition = cameraPosition;
        return this;
    }

    /**
     * Specifies the accesstoken associated with a map view.
     *
     * @param accessToken Token to be used to access the service
     * @return This
     */
    public MapboxMapOptions accessToken(String accessToken) {
        this.accessToken = accessToken;
        return this;
    }

    /**
     * Specifies the style url associated with a map view.
     *
     * @param styleUrl Url to be used to load a style
     * @return This
     */
    public MapboxMapOptions styleUrl(String styleUrl) {
        style = styleUrl;
        return this;
    }

    /**
     * Specifies the used debug type for a map view.
     *
     * @param enabled True is debug is enabled
     * @return This
     */
    public MapboxMapOptions debugActive(boolean enabled) {
        debugActive = enabled;
        return this;
    }

    /**
     * Specifies the used minimum zoom level for a map view.
     *
     * @param minZoom Zoom level to be used
     * @return This
     */
    public MapboxMapOptions minZoom(float minZoom) {
        this.minZoom = minZoom;
        return this;
    }

    /**
     * Specifies the used maximum zoom level for a map view.
     *
     * @param maxZoom Zoom level to be used
     * @return This
     */
    public MapboxMapOptions maxZoom(float maxZoom) {
        this.maxZoom = maxZoom;
        return this;
    }

    /**
     * Specifies the visibility state of a compass for a map view.
     *
     * @param enabled True and compass is shown
     * @return This
     */
    public MapboxMapOptions compassEnabled(boolean enabled) {
        compassEnabled = enabled;
        return this;
    }

    /**
     * Specifies the gravity state of compass for a map view.
     *
     * @param gravity see {@link android.view.Gravity}
     * @return This
     */
    public MapboxMapOptions compassGravity(int gravity) {
        compassGravity = gravity;
        return this;
    }

    /**
     * Specifies the margin state of compass for a map view
     *
     * @param margins 4 long array for LTRB margins
     * @return This
     */
    public MapboxMapOptions compassMargins(int[] margins) {
        compassMargins = margins;
        return this;
    }

    /**
     * Specifies the visibility state of a logo for a map view.
     *
     * @param enabled True and logo is shown
     * @return This
     */
    public MapboxMapOptions logoEnabled(boolean enabled) {
        logoEnabled = enabled;
        return this;
    }

    /**
     * Specifies the gravity state of logo for a map view.
     *
     * @param gravity see {@link android.view.Gravity}
     * @return This
     */
    public MapboxMapOptions logoGravity(int gravity) {
        logoGravity = gravity;
        return this;
    }

    /**
     * Specifies the margin state of logo for a map view
     *
     * @param margins 4 long array for LTRB margins
     * @return This
     */
    public MapboxMapOptions logoMargins(int[] margins) {
        logoMargins = margins;
        return this;
    }

    /**
     * Specifies the visibility state of a attribution for a map view.
     *
     * @param enabled True and attribution is shown
     * @return This
     */
    public MapboxMapOptions attributionEnabled(boolean enabled) {
        attributionEnabled = enabled;
        return this;
    }

    /**
     * Specifies the gravity state of attribution for a map view.
     *
     * @param gravity see {@link android.view.Gravity}
     * @return This
     */
    public MapboxMapOptions attributionGravity(int gravity) {
        attributionGravity = gravity;
        return this;
    }

    /**
     * Specifies the margin state of attribution for a map view
     *
     * @param margins 4 long array for LTRB margins
     * @return This
     */
    public MapboxMapOptions attributionMargins(int[] margins) {
        attributionMargins = margins;
        return this;
    }

    /**
     * Specifies if the rotate gesture is enabled for a map view.
     *
     * @param enabled True and gesture will be enabled
     * @return This
     */
    public MapboxMapOptions rotateGesturesEnabled(boolean enabled) {
        rotateGesturesEnabled = enabled;
        return this;
    }

    /**
     * Specifies if the scroll gesture is enabled for a map view.
     *
     * @param enabled True and gesture will be enabled
     * @return This
     */
    public MapboxMapOptions scrollGesturesEnabled(boolean enabled) {
        scrollGesturesEnabled = enabled;
        return this;
    }

    /**
     * Specifies if the tilt gesture is enabled for a map view.
     *
     * @param enabled True and gesture will be enabled
     * @return This
     */
    public MapboxMapOptions tiltGesturesEnabled(boolean enabled) {
        tiltGesturesEnabled = enabled;
        return this;
    }

    /**
     * Specifies if the zoom controls are enabled for a map view.
     *
     * @param enabled True and gesture will be enabled
     * @return This
     */
    public MapboxMapOptions zoomControlsEnabled(boolean enabled) {
        zoomControlsEnabled = enabled;
        return this;
    }

    /**
     * Specifies if the zoom gesture is enabled for a map view.
     *
     * @param enabled True and gesture will be enabled
     * @return This
     */
    public MapboxMapOptions zoomGesturesEnabled(boolean enabled) {
        zoomGesturesEnabled = enabled;
        return this;
    }

    /**
     * Specifies if the user location view is enabled for a map view.
     *
     * @param locationEnabled True and gesture will be enabled
     * @return This
     */
    public MapboxMapOptions locationEnabled(boolean locationEnabled) {
        this.locationEnabled = locationEnabled;
        return this;
    }

    /**
     * Get the current configured initial camera position for a map view.
     *
     * @return CameraPosition to be initially used.
     */
    public CameraPosition getCamera() {
        return cameraPosition;
    }

    /**
     * Get the current configured min zoom for a map view.
     *
     * @return Mininum zoom level to be used.
     */
    public float getMinZoom() {
        return minZoom;
    }

    /**
     * Get the current configured maximum zoom for a map view.
     *
     * @return Maximum zoom to be used.
     */
    public float getMaxZoom() {
        return maxZoom;
    }

    /**
     * Get the current configured visibility state for compass for a map view.
     *
     * @return Visibility state of the compass
     */
    public boolean getCompassEnabled() {
        return compassEnabled;
    }

    /**
     * Get the current configured gravity state for compass for a map view.
     *
     * @return Gravity state of the compass
     */
    public int getCompassGravity() {
        return compassGravity;
    }

    /**
     * Get the current configured margins for compass for a map view.
     *
     * @return Margins state of the compass
     */
    public int[] getCompassMargins() {
        return compassMargins;
    }

    /**
     * Get the current configured visibility state for compass for a map view.
     *
     * @return Visibility state of the compass
     */
    public boolean getLogoEnabled() {
        return logoEnabled;
    }

    /**
     * Get the current configured gravity state for logo for a map view.
     *
     * @return Gravity state of the logo
     */
    public int getLogoGravity() {
        return logoGravity;
    }

    /**
     * Get the current configured margins for logo for a map view.
     *
     * @return Margins state of the logo
     */
    public int[] getLogoMargins() {
        return logoMargins;
    }

    /**
     * Get the current configured access token for a map view.
     *
     * @return Access token to be used.
     */
    public String getAccessToken() {
        return accessToken;
    }

    /**
     * Get the current configured style url for a map view.
     *
     * @return Style url to be used.
     */
    public String getStyle() {
        return style;
    }

    /**
     * Get the current configured rotate gesture state for a map view.
     *
     * @return True indicates gesture is enabled
     */
    public boolean getRotateGesturesEnabled() {
        return rotateGesturesEnabled;
    }

    /**
     * Get the current configured scroll gesture state for a map view.
     *
     * @return True indicates gesture is enabled
     */
    public boolean getScrollGesturesEnabled() {
        return scrollGesturesEnabled;
    }

    /**
     * Get the current configured tilt gesture state for a map view.
     *
     * @return True indicates gesture is enabled
     */
    public boolean getTiltGesturesEnabled() {
        return tiltGesturesEnabled;
    }

    /**
     * Get the current configured zoom controls state for a map view.
     *
     * @return True indicates gesture is enabled
     */
    public boolean getZoomControlsEnabled() {
        return zoomControlsEnabled;
    }

    /**
     * Get the current configured zoom gesture state for a map view.
     *
     * @return True indicates gesture is enabled
     */
    public boolean getZoomGesturesEnabled() {
        return zoomGesturesEnabled;
    }

    /**
     * Get the current configured visibility state for attribution for a map view.
     *
     * @return Visibility state of the attribution
     */
    public boolean getAttributionEnabled() {
        return attributionEnabled;
    }

    /**
     * Get the current configured gravity state for attribution for a map view.
     *
     * @return Gravity state of the logo
     */
    public int getAttributionGravity() {
        return attributionGravity;
    }

    /**
     * Get the current configured margins for attribution for a map view.
     *
     * @return Margins state of the logo
     */
    public int[] getAttributionMargins() {
        return attributionMargins;
    }

    /**
     * Get the current configured user location view state for a map view.
     *
     * @return True and user location will be shown
     */
    public boolean getLocationEnabled() {
        return locationEnabled;
    }

    /**
     * Get the current configured debug state for a map view.
     *
     * @return True indicates debug is enabled.
     */
    public boolean getDebugActive() {
        return debugActive;
    }

    public static final Parcelable.Creator<MapboxMapOptions> CREATOR
            = new Parcelable.Creator<MapboxMapOptions>() {
        public MapboxMapOptions createFromParcel(Parcel in) {
            return new MapboxMapOptions(in);
        }

        public MapboxMapOptions[] newArray(int size) {
            return new MapboxMapOptions[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeParcelable(cameraPosition, flags);
        dest.writeByte((byte) (debugActive ? 1 : 0));

        dest.writeByte((byte) (compassEnabled ? 1 : 0));
        dest.writeInt(compassGravity);
        dest.writeIntArray(compassMargins);

        dest.writeByte((byte) (logoEnabled ? 1 : 0));
        dest.writeInt(logoGravity);
        dest.writeIntArray(logoMargins);

        dest.writeByte((byte) (attributionEnabled ? 1 : 0));
        dest.writeInt(attributionGravity);
        dest.writeIntArray(attributionMargins);

        dest.writeFloat(minZoom);
        dest.writeFloat(maxZoom);

        dest.writeByte((byte) (rotateGesturesEnabled ? 1 : 0));
        dest.writeByte((byte) (scrollGesturesEnabled ? 1 : 0));
        dest.writeByte((byte) (tiltGesturesEnabled ? 1 : 0));
        dest.writeByte((byte) (zoomControlsEnabled ? 1 : 0));
        dest.writeByte((byte) (zoomGesturesEnabled ? 1 : 0));

        dest.writeByte((byte) (locationEnabled ? 1 : 0));

        dest.writeString(style);
        dest.writeString(accessToken);
    }
}