Location Wikipedia¶
In this guide you will learn how to render
the wikipedia page for a point of interest (POI) on the map.
Setup¶
First, get an API key token, see the
Getting Started guide.
Download the LocationWikipedia 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
An android device should be connected via USB cable.
Press SHIFT+F10 to compile, install and run the example on the
android device.
How it works¶
You can open the MainActivity.kt file to see how the wikipedia page is obtained and displayed.
1override fun onCreate(savedInstanceState: Bundle?) {
2 super.onCreate(savedInstanceState)
3 setContentView(R.layout.activity_main)
4 locationName = findViewById(R.id.locationName)
5 locationWiki = findViewById(R.id.locationWiki)
6 progressBar = findViewById(R.id.progressBar)
7 SdkSettings.onMapDataReady = onMapDataReady@{ isReady ->
8 if (!isReady) return@onMapDataReady
9 search()
10 }
11 SdkSettings.onApiTokenRejected = {
12 // Check the TOKEN provided in the AndroidManifest.xml file.
13 showToast("TOKEN REJECTED")
14 }
15 // This step of initialization is mandatory if you want
16 // to use the SDK without a map.
17 if (!GemSdk.initSdkWithDefaults(this)) {
18 finish()
19 }
20 if (!Util.isInternetConnected(this)) {
21 Toast.makeText(this, "You must be connected to internet!",
22 Toast.LENGTH_LONG).show()
23 }
24 }
The
MainActivity
overrides the onCreate
function, which checks that the API
token is valid, initializes the maps SDK, and checks that internet access is available.As soon as the map is ready, the
search()
function is called automatically.1private fun search() = SdkCall.execute {
2 val name = "Eiffel Tower"
3 val coordinates = Coordinates(0.0, 0.0)
4 searchService.searchByFilter(name, coordinates)
5}
The text string containing the name to search for is set in the
search()
function,
and searchService.searchByFilter
is called.Coordinates are not necessary and can be set to 0.
1private val searchService = SearchService(
2 onStarted = {
3 progressBar?.visibility = View.VISIBLE
4 },
5
6 onCompleted = { results, errorCode, _ ->
7 progressBar?.visibility = View.GONE
8
9 if (errorCode == GemError.NoError) {
10 if (results.isNotEmpty()) {
11 val name = SdkCall.execute { results[0].name }
12 locationName?.text = name
13 requestWiki(results[0])
14 } else {
15 // The search completed without errors,
16 // but there were no results found.
17 showToast("No results!")
18 }
19 }
20 }
21 )
When the search completes, the first result (at index 0)
is passed to
requestWiki(results[0])
to load the wikipedia page,
unless the search found no results, in which case a message to that
effect is displayed instead: showToast("No results!")