Skip to main content
GuidesAPI ReferenceExamples

Search Nearby

|

In this guide you will learn how to search for points of interest (POIs) near the current location.

Nearby locations

If location permission was given by the user, then the search() function can use the SearchService to search around the position given by the PositionService.

MainActivity.kt
private fun search() = SdkCall.execute {
// If one of the location permissions is granted, we can do the search around action.
val hasPermissions =
PermissionsHelper.hasPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
if (!hasPermissions) return@execute

// Cancel any search that is in progress now.
searchService.cancelSearch()

PositionService.getCurrentPosition()?.let {
reference = it

// Search around position using the provided search preferences and/ or filter.
searchService.searchAroundPosition(it)
}
}

Then it starts the search with the API function searchService.searchAroundPosition(it) and when the search completes, the searchService.onCompleted callback function is invoked to put the results in a list for display:

MainActivity.kt
onCompleted = onCompleted@{ results, errorCode, _ ->
progressBar.visibility = View.GONE

when (errorCode)
{
GemError.NoError ->
{
// No error encountered, we can handle the results.
if (results.isNotEmpty())
{
reference?.let { listView.adapter = CustomAdapter(it, results, imageSize) }
}
else
{
// The search completed without errors, but there were no results found.
showDialog("No results!")
}
}

GemError.Cancel ->
{
// The search action was cancelled.
}
else ->
{
// There was a problem at computing the search operation.
showDialog("Search service error: ${GemError.getMessage(errorCode)}")
}
}
}