Skip to content

Commit

Permalink
[location-component] Add isPointOnLocationPuck API.
Browse files Browse the repository at this point in the history
  • Loading branch information
pengdev committed Feb 15, 2021
1 parent b58db11 commit e93db26
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.mapbox.maps.testapp.examples
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.content.res.AppCompatResources
import com.mapbox.maps.CameraOptions
Expand Down Expand Up @@ -34,9 +35,27 @@ class LocationComponentActivity : AppCompatActivity() {
setContentView(R.layout.activity_location_component)
locationPermissionHelper = LocationPermissionHelper(this)
locationPermissionHelper.checkPermissions {
mapView.getMapboxMap().loadStyleUri(Style.MAPBOX_STREETS) {
mapView.getMapboxMap().loadStyleUri(Style.MAPBOX_STREETS) { _ ->
// Disable scroll gesture, since we are updating the camera position based on the indicator location.
mapView.getGesturesPlugin().scrollEnabled = false
mapView.getGesturesPlugin().addOnMapClickListener { point ->
mapView.getLocationComponentPlugin()
.isPointOnLocationPuck(point) { isPointOnLocationPuck ->
if (isPointOnLocationPuck) {
Toast.makeText(this, "Clicked on location puck", Toast.LENGTH_SHORT).show()
}
}
true
}
mapView.getGesturesPlugin().addOnMapLongClickListener { point ->
mapView.getLocationComponentPlugin()
.isPointOnLocationPuck(point) { isPointOnLocationPuck ->
if (isPointOnLocationPuck) {
Toast.makeText(this, "Long-clicked on location puck", Toast.LENGTH_SHORT).show()
}
}
true
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import android.util.AttributeSet
import androidx.annotation.VisibleForTesting
import androidx.annotation.VisibleForTesting.PRIVATE
import com.mapbox.geojson.Point
import com.mapbox.maps.RenderedQueryOptions
import com.mapbox.maps.StyleManagerInterface
import com.mapbox.maps.plugin.delegates.MapDelegateProvider
import com.mapbox.maps.plugin.delegates.MapPluginProviderDelegate
import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.LOCATION_INDICATOR_LAYER
import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.MODEL_LAYER
import com.mapbox.maps.plugin.locationcomponent.animators.PuckAnimatorManager
import com.mapbox.maps.plugin.locationcomponent.generated.LocationComponentAttributeParser
import com.mapbox.maps.plugin.locationcomponent.generated.LocationComponentSettings
Expand Down Expand Up @@ -84,6 +87,36 @@ class LocationComponentPluginImpl : LocationComponentPlugin, LocationConsumer,
onIndicatorBearingChangedListener.remove(listener)
}

/**
* Check is a given point is on the rendered location puck.
*
* @param point the point to validate
* @param listener Listener that gets invoked when the validation finished.
*/
override fun isPointOnLocationPuck(point: Point, listener: IsPointOnLocationPuckListener) {
delegateProvider.mapFeatureQueryDelegate.queryRenderedFeatures(
delegateProvider.mapProjectionDelegate.pixelForCoordinate(point),
RenderedQueryOptions(
listOf(
LOCATION_INDICATOR_LAYER,
MODEL_LAYER
),
null
)
) { expected ->
expected.value?.let {
if (it.isNotEmpty()) {
listener.isPointOnLocationPuck(true)
} else {
listener.isPointOnLocationPuck(false)
}
}
expected.error?.let {
throw RuntimeException(it)
}
}
}

@VisibleForTesting(otherwise = PRIVATE)
internal val indicatorBearingChangedListener = OnIndicatorBearingChangedListener {
for (listener in onIndicatorBearingChangedListener) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.mapbox.maps.plugin.locationcomponent

/**
* Listener that gets invoked when the is point on location puck validation finished.
*/
fun interface IsPointOnLocationPuckListener {
/**
* This method is called when the is point on location puck validation finished.
*
* @param isOnLocationPuck true if the given point is on the location puck, false otherwise.
*/
fun isPointOnLocationPuck(isOnLocationPuck: Boolean)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mapbox.maps.plugin.locationcomponent

import com.mapbox.geojson.Point
import com.mapbox.maps.plugin.*
import com.mapbox.maps.plugin.locationcomponent.generated.LocationComponentSettingsInterface

Expand Down Expand Up @@ -51,4 +52,12 @@ interface LocationComponentPlugin :
* @param listener Listener that gets invoked when indicator bearing changes.
*/
fun removeOnIndicatorBearingChangedListener(listener: OnIndicatorBearingChangedListener)

/**
* Check is a given point is on the rendered location puck.
*
* @param point the point to validate
* @param listener Listener that gets invoked when the validation finished.
*/
fun isPointOnLocationPuck(point: Point, listener: IsPointOnLocationPuckListener)
}

0 comments on commit e93db26

Please sign in to comment.