summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraPosition.java
blob: 004e7f5f84a7455a18dc74488ea1ed257457da8e (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
package com.mapbox.mapboxsdk.camera;

import android.content.res.TypedArray;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.FloatRange;

import com.mapbox.mapboxsdk.R;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.constants.MathConstants;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.CameraUpdateFactory;
import com.mapbox.mapboxsdk.utils.MathUtils;

public final class CameraPosition implements Parcelable {

    public static final Parcelable.Creator<CameraPosition> CREATOR
            = new Parcelable.Creator<CameraPosition>() {
        public CameraPosition createFromParcel(Parcel in) {
            float bearing = in.readFloat();
            LatLng target = in.readParcelable(LatLng.class.getClassLoader());
            float tilt = in.readFloat();
            float zoom = in.readFloat();
            return new CameraPosition(target, zoom, tilt, bearing);
        }

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


    /**
     * Direction that the camera is pointing in, in degrees clockwise from north.
     */
    public final float bearing;

    /**
     * The location that the camera is pointing at.
     */
    public final LatLng target;

    /**
     * The angle, in degrees, of the camera angle from the nadir (directly facing the Earth). See tilt(float) for details of restrictions on the range of values.
     */
    public final float tilt;

    /**
     * Zoom level near the center of the screen. See zoom(float) for the definition of the camera's zoom level.
     */
    public final float zoom;

    /**
     * Constructs a CameraPosition.
     *
     * @param target  The target location to align with the center of the screen.
     * @param zoom    Zoom level at target. See zoom(float) for details of restrictions.
     * @param tilt    The camera angle, in degrees, from the nadir (directly down). See tilt(float) for details of restrictions.
     * @param bearing Direction that the camera is pointing in, in degrees clockwise from north. This value will be normalized to be within 0 degrees inclusive and 360 degrees exclusive.
     * @throws NullPointerException     if target is null
     * @throws IllegalArgumentException if tilt is outside the range of 0 to 90 degrees inclusive.
     */
    CameraPosition(LatLng target, float zoom, float tilt, float bearing) {
        this.target = target;
        this.bearing = bearing;
        this.tilt = tilt;
        this.zoom = zoom;
    }

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

    @Override
    public void writeToParcel(Parcel out, int flags) {
        out.writeFloat(bearing);
        out.writeParcelable(target, flags);
        out.writeFloat(tilt);
        out.writeFloat(zoom);
    }

    @Override
    public String toString() {
        return "Target: " + target + ", Zoom:" + zoom + ", Bearing:" + bearing + ", Tilt:" + tilt;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }

        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        CameraPosition cameraPosition = (CameraPosition) o;
        if (!target.equals(cameraPosition.target)) {
            return false;
        } else if (zoom != cameraPosition.zoom) {
            return false;
        } else if (tilt != cameraPosition.tilt) {
            return false;
        } else if (bearing != cameraPosition.bearing) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + (target != null ? target.hashCode() : 0);
        return result;
    }

    /**
     * Builds camera position.
     */
    public static final class Builder {

        private float bearing = -1;
        private LatLng target = null;
        private float tilt = -1;
        private float zoom = -1;
        private boolean isRadiant;

        /**
         * Creates an empty builder.
         */
        public Builder() {
            super();
        }

        /**
         * Creates a builder for building CameraPosition objects using radiants.
         *
         * @param isRadiant
         */
        public Builder(boolean isRadiant) {
            this.isRadiant = isRadiant;
        }

        /**
         * Create Builder with an existing CameraPosition data.
         *
         * @param previous Existing CameraPosition values to use
         */
        public Builder(CameraPosition previous) {
            super();
            if (previous != null) {
                this.bearing = previous.bearing;
                this.target = previous.target;
                this.tilt = previous.tilt;
                this.zoom = previous.zoom;
            }
        }

        /**
         * Create Builder with an existing CameraPosition data.
         *
         * @param typedArray TypedArray containgin attribute values
         */
        public Builder(TypedArray typedArray) {
            super();
            if (typedArray != null) {
                this.bearing = typedArray.getFloat(R.styleable.MapView_direction, 0.0f);
                double lat = typedArray.getFloat(R.styleable.MapView_center_latitude, 0.0f);
                double lng = typedArray.getFloat(R.styleable.MapView_center_longitude, 0.0f);
                this.target = new LatLng(lat, lng);
                this.tilt = typedArray.getFloat(R.styleable.MapView_tilt, 0.0f);
                this.zoom = typedArray.getFloat(R.styleable.MapView_zoom, 0.0f);
            }
        }

        /**
         * Create Builder from an existing CameraPositionUpdate update.
         *
         * @param update Update containing camera options
         */
        public Builder(CameraUpdateFactory.CameraPositionUpdate update) {
            super();
            if (update != null) {
                this.bearing = update.getBearing();
                this.target = update.getTarget();
                this.tilt = update.getTilt();
                this.zoom = update.getZoom();
            }
        }

        /**
         * Create Builder from an existing CameraPositionUpdate update.
         *
         * @param update Update containing camera options
         */
        public Builder(CameraUpdateFactory.ZoomUpdate update) {
            super();
            if (update != null) {
                this.zoom = update.getZoom();
            }
        }

        /**
         * Sets the direction that the camera is pointing in, in degrees clockwise from north.
         *
         * @param bearing Bearing
         * @return Builder
         */
        public Builder bearing(float bearing) {
            if(isRadiant){
                this.bearing = bearing;
            }else{
                // converting degrees to radiant
                this.bearing = (float) (-bearing * MathConstants.DEG2RAD);
            }
            return this;
        }

        /**
         * Builds a CameraPosition.
         *
         * @return CameraPosition
         */
        public CameraPosition build() {
            return new CameraPosition(target, zoom, tilt, bearing);
        }

        /**
         * Sets the location that the camera is pointing at.
         *
         * @param location Location
         * @return Builder
         */
        public Builder target(LatLng location) {
            this.target = location;
            return this;
        }

        /**
         * Set the tilt
         *
         * @param tilt Tilt value
         * @return Builder
         */
        @FloatRange(from = 0.0, to = 60.0)
        public Builder tilt(float tilt) {
            if(isRadiant){
                this.tilt = tilt;
            }else {
                // converting degrees to radiant
                this.tilt = (float) (MathUtils.clamp(tilt, MapboxConstants.MINIMUM_TILT, MapboxConstants.MAXIMUM_TILT) * MathConstants.DEG2RAD);
            }
            return this;
        }

        /**
         * Set the zoom
         *
         * @param zoom Zoom value
         * @return Builder
         */
        public Builder zoom(float zoom) {
            this.zoom = zoom;
            return this;
        }
    }
}