Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extension function for locationComponent to support show and hide arrow bearing image. #1012

Merged
merged 5 commits into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ private void locationComponentSettings(LocationPuck locationPuck) {
locationComponentSettings = new LocationComponentSettings(true, true, Color.BLACK, 1f, "id", "id", locationPuck);
}

private void locationComponent(Context context, MapView mapView) {
LocationComponentPlugin locationComponent = LocationComponentUtils.getLocationComponent(mapView);
locationComponent.setLocationPuck(LocationComponentUtils.createDefault2DPuck(locationComponent, context));
locationComponent.setLocationPuck(LocationComponentUtils.createDefault2DPuck(locationComponent, context, true));
}

private void gesturesSettings(ScrollMode scrollMode, ScreenCoordinate screenCoordinate) {
GesturesSettings gesturesSettings = new GesturesSettings();
gesturesSettings = new GesturesSettings(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import com.mapbox.maps.plugin.LocationPuck2D
import com.mapbox.maps.plugin.LocationPuck3D
import com.mapbox.maps.plugin.PuckBearingSource
import com.mapbox.maps.plugin.gestures.gestures
import com.mapbox.maps.plugin.locationcomponent.OnIndicatorPositionChangedListener
import com.mapbox.maps.plugin.locationcomponent.location
import com.mapbox.maps.plugin.locationcomponent.*
import com.mapbox.maps.testapp.R
import com.mapbox.maps.testapp.databinding.ActivityLocationComponentBinding
import com.mapbox.maps.testapp.utils.LocationPermissionHelper
Expand Down Expand Up @@ -86,6 +85,22 @@ class LocationComponentActivity : AppCompatActivity() {
binding.mapView.location.enabled = true
return true
}
R.id.action_show_bearing -> {
if (binding.mapView.location.locationPuck is LocationPuck2D) {
binding.mapView.location.apply {
locationPuck = createDefault2DPuck(this@LocationComponentActivity, withBearing = true)
}
}
return true
}
R.id.action_hide_bearing -> {
if (binding.mapView.location.locationPuck is LocationPuck2D) {
binding.mapView.location.apply {
locationPuck = createDefault2DPuck(this@LocationComponentActivity)
}
}
return true
}
R.id.heading -> {
binding.mapView.location.updateSettings { puckBearingSource = PuckBearingSource.HEADING }
item.isChecked = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class LocationComponentAnimationActivity : AppCompatActivity() {
bearingImage = AppCompatResources.getDrawable(
this@LocationComponentAnimationActivity,
R.drawable.mapbox_mylocation_icon_bearing,
)
),
)
}
}
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/menu/menu_location_component.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
android:title="@string/location_enable_component"
app:showAsAction="never"/>

<item android:id="@+id/action_show_bearing"
android:title="@string/location_show_bearing"
app:showAsAction="never"/>

<item android:id="@+id/action_hide_bearing"
android:title="@string/location_hide_bearing"
app:showAsAction="never"/>

<group
android:id="@+id/group"
android:checkableBehavior="single">
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
<string name="toggle_custom_map_style">Toggle custom Map style</string>
<string name="location_disable_component">Disable Component</string>
<string name="location_enable_component">Enable Component</string>
<string name="location_show_bearing">Show bearing image</string>
<string name="location_hide_bearing">Hide bearing image</string>
<string name="location_stop_pulsing">Stop pulsing</string>
<string name="location_start_pulsing">Start pulsing</string>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

package com.mapbox.maps.plugin.locationcomponent

import android.content.Context
import androidx.core.content.res.ResourcesCompat
import com.mapbox.maps.plugin.LocationPuck2D
import com.mapbox.maps.plugin.Plugin
import com.mapbox.maps.plugin.delegates.MapPluginProviderDelegate

Expand All @@ -10,4 +13,49 @@ import com.mapbox.maps.plugin.delegates.MapPluginProviderDelegate
*/
val MapPluginProviderDelegate.location: LocationComponentPlugin
@JvmName("getLocationComponent")
get() = this.getPlugin(Plugin.MAPBOX_LOCATION_COMPONENT_PLUGIN_ID)!!
get() = this.getPlugin(Plugin.MAPBOX_LOCATION_COMPONENT_PLUGIN_ID)!!

/**
* Create a [LocationPuck2D] instance with or without an arrow bearing image.
* @param context the context of application
* @param withBearing if ture, the location puck will show an arrow bearing image, default is false.
*/
@JvmOverloads
fun LocationComponentPlugin.createDefault2DPuck(
context: Context,
withBearing: Boolean = false
): LocationPuck2D {
if (withBearing) {
return LocationPuck2D(
topImage = ResourcesCompat.getDrawable(context.resources, R.drawable.mapbox_user_icon, null),
bearingImage = ResourcesCompat.getDrawable(
context.resources,
R.drawable.mapbox_user_bearing_icon,
null
),
shadowImage = ResourcesCompat.getDrawable(
context.resources,
R.drawable.mapbox_user_stroke_icon,
null
)
)
} else {
return LocationPuck2D(
topImage = ResourcesCompat.getDrawable(
context.resources,
R.drawable.mapbox_user_icon,
null
),
bearingImage = ResourcesCompat.getDrawable(
context.resources,
R.drawable.mapbox_user_stroke_icon,
null
),
shadowImage = ResourcesCompat.getDrawable(
context.resources,
R.drawable.mapbox_user_icon_shadow,
null
)
)
}
Comment on lines +28 to +60
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

val bearingImageId = if (withBearing) R.drawable.mapbox_user_bearing_icon else R.drawable.mapbox_user_stroke_icon

val shadowImage =  if (withBearing) R.drawable.mapbox_user_stroke_icon else R.drawable.mapbox_user_icon_shadow

return LocationPuck2D(
      topImage = ResourcesCompat.getDrawable(context.resources, R.drawable.mapbox_user_icon, null),
      bearingImage = ResourcesCompat.getDrawable(
        context.resources,
        bearingImageId,
        null
      ),
      shadowImage = ResourcesCompat.getDrawable(
        context.resources,
        shadowImage,
        null
      )
    )

also small extension for context:

fun Context.getCompatDrawable(resId:Int) = ResourcesCompat.getDrawable(
        this,
        resId,
        null
      )

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ internal class LocationIndicatorLayerRenderer(
LocationLayerRenderer {
private var style: StyleInterface? = null
private var layer = layerSourceProvider.getLocationIndicatorLayer()

override fun initializeComponents(style: StyleInterface) {
this.style = style
setupBitmaps()
Expand Down Expand Up @@ -78,6 +77,7 @@ internal class LocationIndicatorLayerRenderer(
?.let { style?.addImage(TOP_ICON, it) }
puckOptions.bearingImage?.let { BitmapUtils.getBitmapFromDrawable(it) }
?.let { style?.addImage(BEARING_ICON, it) }

puckOptions.shadowImage?.let { BitmapUtils.getBitmapFromDrawable(it) }
?.let { style?.addImage(SHADOW_ICON, it) }
layer.topImage(TOP_ICON)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class LocationIndicatorLayerRendererTest {
any()
)
}
verify {
verify(exactly = 1) {
style.addImage(
BEARING_ICON,
any()
Expand Down