@@ -11,25 +11,22 @@ import androidx.compose.runtime.*
1111import androidx.compose.ui.Alignment
1212import androidx.compose.ui.Modifier
1313import androidx.compose.ui.graphics.Color
14- import androidx.compose.ui.platform.LocalContext
1514import androidx.compose.ui.res.painterResource
1615import androidx.compose.ui.text.font.FontWeight
1716import androidx.compose.ui.unit.dp
1817import androidx.compose.ui.unit.sp
19- import com.mapbox.common.location.LocationServiceFactory
20- import com.mapbox.geojson.BoundingBox
21- import com.mapbox.geojson.Point
22- import com.mapbox.maps.dsl.cameraOptions
2318import com.mapbox.maps.extension.compose.animation.viewport.MapViewportState
2419import com.mapbox.maps.plugin.animation.MapAnimationOptions
20+ import com.mapbox.maps.dsl.cameraOptions
21+ import com.mapbox.search.SearchSelectionCallback
2522import com.mapbox.search.ApiType
2623import com.mapbox.search.ResponseInfo
2724import com.mapbox.search.SearchEngine
2825import com.mapbox.search.SearchEngineSettings
2926import com.mapbox.search.SearchOptions
30- import com.mapbox.search.SearchSelectionCallback
3127import com.mapbox.search.result.SearchResult
3228import com.mapbox.search.result.SearchSuggestion
29+ import com.mapbox.search.result.SearchSuggestionType.Category
3330
3431
3532@OptIn(ExperimentalMaterial3Api ::class )
@@ -39,23 +36,14 @@ fun SearchScreen(
3936 modifier : Modifier = Modifier ,
4037 onSuggestionSelected : (SearchResult ) -> Unit
4138) {
42- val context = LocalContext .current;
4339 var query by remember { mutableStateOf(" " ) }
4440 var suggestions by remember { mutableStateOf<List <SearchSuggestion >>(emptyList()) }
4541
46- val locationProvider = LocationServiceFactory .getOrCreate()
47- .getDeviceLocationProvider(null )
48- .value
49-
50- val searchEngineSettings = SearchEngineSettings (
51- locationProvider = locationProvider
52- )
53-
5442 // Get SearchEngine instance
5543 val searchEngine = remember {
5644 SearchEngine .createSearchEngine(
5745 ApiType .SEARCH_BOX ,
58- searchEngineSettings
46+ SearchEngineSettings ()
5947 )
6048 }
6149
@@ -73,12 +61,7 @@ fun SearchScreen(
7361 searchEngine.search(
7462 newQuery,
7563 SearchOptions (
76- // proximity = Point.fromLngLat(-79.35954, 43.65050), // Proximity to Toronto's Distillery District
77- limit = 10 ,
78- // boundingBox = BoundingBox.fromPoints(
79- // Point.fromLngLat(-79.49555, 43.60698),
80- // Point.fromLngLat(-79.29422, 43.75953)
81- // ) // Bounding Box of the Greater Toronto Area
64+ limit = 10
8265 ),
8366 callback = object : com.mapbox.search.SearchSuggestionsCallback {
8467 override fun onSuggestions (
@@ -111,6 +94,7 @@ fun SearchScreen(
11194 shape = RoundedCornerShape (8 .dp)
11295 )
11396
97+
11498 Spacer (modifier = Modifier .height(16 .dp))
11599
116100 // Suggestions list anchored right below the TextField
@@ -121,7 +105,7 @@ fun SearchScreen(
121105 shape = RoundedCornerShape (8 .dp),
122106 modifier = Modifier
123107 .fillMaxWidth()
124- .padding(top = 8 .dp)
108+ .padding(top= 8 .dp, start = 16 .dp, end = 16 .dp)
125109 .heightIn(max = 500 .dp)
126110 ) {
127111 val scrollState = rememberScrollState()
@@ -130,11 +114,17 @@ fun SearchScreen(
130114 .padding(16 .dp)
131115 .verticalScroll(scrollState)
132116 ) {
133- suggestions.forEachIndexed { index, suggestion ->
117+ // Filter out category suggestion types
118+ val filteredSuggestions = suggestions.filter { it.type !is Category }
119+
120+ filteredSuggestions.forEachIndexed { index, suggestion ->
134121
135122 val distanceKm = suggestion.distanceMeters?.div(1000.0 )
136123 val addressText = suggestion.fullAddress
137- ? : listOfNotNull(suggestion.address?.region, suggestion.address?.country).joinToString(" , " )
124+ ? : listOfNotNull(
125+ suggestion.address?.region,
126+ suggestion.address?.country
127+ ).joinToString(" , " )
138128
139129 Row (
140130 modifier = Modifier
@@ -182,7 +172,7 @@ fun SearchScreen(
182172 }
183173 }
184174 if (index < suggestions.lastIndex) {
185- Divider (color = Color .LightGray )
175+ HorizontalDivider (color = Color .LightGray )
186176 }
187177 }
188178
@@ -208,20 +198,16 @@ fun handleSuggestionSelection(
208198 // When user selects a suggestion:
209199 onSuggestionSelected(result)
210200 val coordinate = result.coordinate
211- if (coordinate != null ) {
212- val camera = cameraOptions {
213- center(coordinate)
214- zoom(14.0 )
215- }
201+ val camera = cameraOptions {
202+ center(coordinate)
203+ zoom(14.0 )
204+ }
216205
217- val animationOptions = MapAnimationOptions .Builder ()
218- .duration(3000L ) // 3 seconds
219- .build()
206+ val animationOptions = MapAnimationOptions .Builder ()
207+ .duration(3000L ) // 3 seconds
208+ .build()
220209
221- mapViewportState.flyTo(camera, animationOptions)
222- } else {
223- Log .w(" Search" , " No coordinate found for result." )
224- }
210+ mapViewportState.flyTo(camera, animationOptions)
225211 }
226212
227213 override fun onResults (
@@ -230,6 +216,18 @@ fun handleSuggestionSelection(
230216 responseInfo : ResponseInfo
231217 ) {
232218 // handle multiple results (category, brand, etc.)
219+ results.forEachIndexed { index, result ->
220+ Log .d(" Result[$index ]" , """
221+ ID: ${result.id}
222+ Name: ${result.name}
223+ Description: ${result.descriptionText ? : " N/A" }
224+ Place: ${result.address?.place ? : " N/A" }
225+ Region: ${result.address?.region ? : " N/A" }
226+ Country: ${result.address?.country ? : " N/A" }
227+
228+ -----
229+ """ .trimIndent())
230+ }
233231 }
234232
235233
0 commit comments