Skip to content

Commit 91d2e35

Browse files
committed
Use puckBearingEnabled prop in Android
1 parent 7fbc756 commit 91d2e35

File tree

1 file changed

+26
-39
lines changed

1 file changed

+26
-39
lines changed

android/src/main/java/com/rnmapbox/rnmbx/components/location/RNMBXNativeUserLocation.kt

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import com.mapbox.maps.MapboxMap
1818
import com.mapbox.maps.Style
1919
import com.mapbox.maps.plugin.LocationPuck2D
2020
import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants
21+
import com.mapbox.maps.plugin.locationcomponent.createDefault2DPuck
2122
import com.mapbox.maps.plugin.locationcomponent.location
2223
import com.mapbox.maps.plugin.locationcomponent.R as LR
2324
import com.rnmapbox.rnmbx.R
@@ -125,26 +126,33 @@ class RNMBXNativeUserLocation(context: Context) : AbstractMapFeature(context), O
125126
private fun _apply(mapView: MapView) {
126127
val location2 = mapView.location2;
127128

128-
if (visible) {
129-
if (images.isEmpty()) {
130-
location2.locationPuck =
131-
makeDefaultLocationPuck2D(mContext, androidRenderMode ?: RenderMode.NORMAL)
132-
} else {
133-
location2.locationPuck = LocationPuck2D(
134-
topImage = images[PuckImagePart.TOP],
135-
bearingImage = images[PuckImagePart.BEARING],
136-
shadowImage = images[PuckImagePart.SHADOW],
137-
scaleExpression = scale?.toJson()
138-
)
129+
val withBearing = puckBearingEnabled ?: when (androidRenderMode ?: RenderMode.NORMAL) {
130+
RenderMode.GPS -> true
131+
RenderMode.COMPASS -> true
132+
RenderMode.NORMAL -> false
133+
}
134+
135+
// Always start with the default puck
136+
location2.locationPuck = createDefault2DPuck(withBearing = withBearing)
137+
138+
// If custom images are provided, overwrite the corresponding fields
139+
if (images.isNotEmpty()) {
140+
(location2.locationPuck as? LocationPuck2D)?.apply {
141+
topImage = images[PuckImagePart.TOP] ?: topImage
142+
bearingImage = images[PuckImagePart.BEARING] ?: bearingImage
143+
shadowImage = images[PuckImagePart.SHADOW] ?: shadowImage
144+
scaleExpression = scale?.toJson() ?: scaleExpression
139145
}
140-
} else {
141-
val empty =
142-
AppCompatResourcesV11.getDrawableImageHolder(mContext, R.drawable.empty)
143-
location2.locationPuck = LocationPuck2D(
144-
topImage = empty,
145-
bearingImage = empty,
146+
}
147+
148+
// If visibility is false, overwrite the images with empty placeholders
149+
if (!visible) {
150+
val empty = AppCompatResourcesV11.getDrawableImageHolder(mContext, R.drawable.empty)
151+
(location2.locationPuck as? LocationPuck2D)?.apply {
152+
topImage = empty
153+
bearingImage = empty
146154
shadowImage = empty
147-
)
155+
}
148156
}
149157

150158
this.puckBearing?.let {
@@ -278,24 +286,3 @@ class RNMBXNativeUserLocation(context: Context) : AbstractMapFeature(context), O
278286
const val LOG_TAG = "RNMBXNativeUserLocation"
279287
}
280288
}
281-
282-
fun makeDefaultLocationPuck2D(context: Context, renderMode: RenderMode): LocationPuck2D {
283-
return LocationPuck2D(
284-
topImage = AppCompatResourcesV11.getDrawableImageHolder(
285-
context,
286-
LR.drawable.mapbox_user_icon
287-
),
288-
bearingImage = AppCompatResourcesV11.getDrawableImageHolder(
289-
context,
290-
when (renderMode) {
291-
RenderMode.GPS -> LR.drawable.mapbox_user_bearing_icon
292-
RenderMode.COMPASS -> LR.drawable.mapbox_user_puck_icon
293-
RenderMode.NORMAL -> LR.drawable.mapbox_user_stroke_icon
294-
}
295-
),
296-
shadowImage = AppCompatResourcesV11.getDrawableImageHolder(
297-
context,
298-
LR.drawable.mapbox_user_icon_shadow
299-
)
300-
);
301-
}

0 commit comments

Comments
 (0)