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

Updating the sample application for better click on map support #278

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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 @@ -36,7 +36,6 @@ import com.mapbox.maps.plugin.annotation.generated.createCircleAnnotationManager
import com.mapbox.maps.plugin.gestures.addOnMapClickListener
import com.mapbox.maps.plugin.locationcomponent.OnIndicatorPositionChangedListener
import com.mapbox.maps.plugin.locationcomponent.location
import com.mapbox.search.ApiType
import com.mapbox.search.ResponseInfo
import com.mapbox.search.SearchEngine
import com.mapbox.search.SearchEngineSettings
Expand Down Expand Up @@ -158,26 +157,28 @@ class MainActivity : AppCompatActivity() {
})
}

// only support for ApiType.SBS
if (BuildConfig.API_TYPE == ApiType.SBS || BuildConfig.API_TYPE == ApiType.SEARCH_BOX) {
mapView.mapboxMap.addOnMapClickListener { point ->
val screenCoords = mapView.mapboxMap.pixelForCoordinate(point)

mapView.mapboxMap.queryRenderedFeatures(
RenderedQueryGeometry(screenCoords),
RenderedQueryOptions(listOf("poi-label"), null)
) {
it.value?.firstOrNull()?.queriedFeature.let { queriedFeature ->
queriedFeature?.feature?.let { feature ->
searchEngineUiAdapter.select(
feature
)
}
mapView.mapboxMap.addOnMapClickListener { point ->
val screenCoords = mapView.mapboxMap.pixelForCoordinate(point)

mapView.mapboxMap.queryRenderedFeatures(
RenderedQueryGeometry(screenCoords),
RenderedQueryOptions(listOf("poi-label"), null)
) {
val queriedFeatures = it.value?.filter { f -> f.queriedFeature.feature.id()?.isNotEmpty() == true }

when {
queriedFeatures.isNullOrEmpty() -> {
searchEngineUiAdapter.reverse(point)
}
else -> {
searchEngineUiAdapter.select(
queriedFeatures.first().queriedFeature.feature
)
}
}

true
}

true
}

mapMarkersManager = MapMarkersManager(mapView)
Expand Down Expand Up @@ -248,7 +249,7 @@ class MainActivity : AppCompatActivity() {
}

override fun onSearchResultsShown(
suggestion: SearchSuggestion,
suggestion: SearchSuggestion?,
results: List<SearchResult>,
responseInfo: ResponseInfo
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import com.mapbox.common.ReachabilityInterface
import com.mapbox.common.location.LocationProvider
import com.mapbox.common.location.LocationServiceFactory
import com.mapbox.geojson.Feature
import com.mapbox.geojson.Point
import com.mapbox.search.ResponseInfo
import com.mapbox.search.ReverseGeoOptions
import com.mapbox.search.SearchCallback
import com.mapbox.search.SearchEngine
import com.mapbox.search.SearchOptions
import com.mapbox.search.SearchResultCallback
Expand All @@ -32,6 +35,7 @@ import com.mapbox.search.common.concurrent.MainThreadWorker
import com.mapbox.search.common.concurrent.SearchSdkMainThreadWorker
import com.mapbox.search.internal.bindgen.UserActivityReporter
import com.mapbox.search.offline.OfflineResponseInfo
import com.mapbox.search.offline.OfflineReverseGeoOptions
import com.mapbox.search.offline.OfflineSearchCallback
import com.mapbox.search.offline.OfflineSearchEngine
import com.mapbox.search.offline.OfflineSearchOptions
Expand Down Expand Up @@ -129,7 +133,7 @@ public class SearchEngineUiAdapter(
retrySearchRequest()
}

private val searchCallback = object : SearchSuggestionsCallback, SearchSelectionCallback, SearchResultCallback {
private val searchCallback = object : SearchSuggestionsCallback, SearchSelectionCallback, SearchResultCallback, SearchCallback {

override fun onSuggestions(
suggestions: List<SearchSuggestion>,
Expand Down Expand Up @@ -166,6 +170,11 @@ public class SearchEngineUiAdapter(
searchListeners.forEach { it.onSearchResultSelected(result, responseInfo) }
}

override fun onResults(results: List<SearchResult>, responseInfo: ResponseInfo) {
showResults(results, responseInfo)
searchListeners.forEach { it.onSearchResultsShown(null, results, responseInfo) }
}

override fun onError(e: Exception) {
if (searchQuery.isNotEmpty()) {
showError(UiError.createFromException(e))
Expand Down Expand Up @@ -358,6 +367,23 @@ public class SearchEngineUiAdapter(
}
}

@UiThread
public fun reverse(
coords: Point,
options: ReverseGeoOptions = ReverseGeoOptions(center = coords)
) {
checkMainThread()

when (isOnlineSearch) {
true -> {
searchEngine.search(options, searchCallback)
}
false -> {
offlineSearchEngine.reverseGeocoding(OfflineReverseGeoOptions(coords), offlineSearchCallback)
}
}
}

private fun onHistoryRecordRemoved(adapterPosition: Int, record: HistoryRecord) {
val newItems = view.adapterItems.toMutableList().apply {
removeAt(adapterPosition)
Expand Down Expand Up @@ -636,7 +662,7 @@ public class SearchEngineUiAdapter(
* @see SearchSelectionCallback.onResults
*/
public fun onSearchResultsShown(
suggestion: SearchSuggestion,
suggestion: SearchSuggestion?,
results: List<SearchResult>,
responseInfo: ResponseInfo
)
Expand Down