×
Search Nearby | Maps SDK for Android | General Magic Search Nearby — Maps & Navigation SDKs documentation
Skip to content

Search Nearby

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

Setup

First, get an API key token, see the Getting Started guide.

Download the Maps & Navigation SDK for Android archive file

Download the WhatsNearby project archive file or clone the project with Git

See the Configure Android Example guide.

Run the example

In Android Studio, from the File menu, select Sync Project with Gradle Files

Android example screenshot
An android device should be connected via USB cable.
Press SHIFT+F10 to compile, install and run the example on the android device.
A text list of results is shown of various points of interest near the current location of the device.

How it works

Android example screenshot

You can open the MainActivity.kt file to see how to search for POIs near the current or a specified location.

 1private fun search()
 2{
 3     // If one of the location permissions is granted, we can do the search around action.
 4     val hasPermissions =
 5         PermissionsHelper.hasPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
 6
 7     if (hasPermissions)
 8     {
 9         Util.getMyPosition()?.let { myPosition ->
10             searchAround(myPosition)
11         }
12     }
13}
14private fun searchAround(reference: Coordinates)
15{
16     SdkCall.execute
17     {
18         // Cancel any search that is in progress now.
19         cancelSearch()
20
21         // Set the necessary preferences.
22         searchService.preferences.setSearchAddresses(true)
23         searchService.preferences.setSearchMapPOIs(true)
24
25         // Search around position using the provided search preferences and/ or filter.
26         searchService.searchAroundPosition(
27             reference, ""
28         )
29     }
30}

If location permission was given by the user, then the search() function can call the searchAround() function, which sets the preferences to search for POIs and addresses.

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

 1     when (val gemError = SdkError.fromInt(reason))
 2     {
 3         SdkError.NoError ->
 4         {
 5             // No error encountered, we can handle the results.
 6             val reference = Util.getMyPosition() ?: return@onCompleted
 7
 8             val adapter = CustomAdapter(reference, results)
 9             listView?.adapter = adapter
10
11             if (results.isEmpty())
12             {
13                 // The search completed without errors, but there were no results found.
14             }
15         }

Android Examples

Maps SDK for Android Examples can be downloaded or cloned with Git