Skip to main content

Search online and offline

GLSearchRequest has one request shape for both transports. Use startOnline for the Globus service or startOffline for maps already registered with GLMapManager.

Add the GLSearch package product on iOS or globus:glsearch:2.0.0 on Android. Android applications must also call GLSearch.Initialize(this) after GLMap activation.

Swift

import GLMap
import GLSearch

let request = GLSearchRequest(
type: .search,
text: "coffee",
center: GLMapGeoPoint(lat: 41.1579, lon: -8.6291),
limit: 50,
locales: ["en", "native"],
categories: nil
)

requestID = request.startOnline { [weak self] results, error in
if let error {
self?.show(error.localizedDescription)
return
}

self?.rows = results?.array() ?? []
self?.tableView.reloadData()
}

Change only the last call for offline search:

requestID = request.startOffline(completion: completion)

Build a table row directly from each result:

let title = result.localizedName(map.localeSettings)?.asString() ?? "Unnamed"
let subtitle = result.searchSecondaryText?.asString()

Both start methods invoke completion on the main queue exactly once. Cancel an obsolete autocomplete request before starting the next one:

if requestID != 0 {
GLSearchRequest.cancel(requestID)
}

Cancellation completes with an ECANCELED error, so handle it separately when it is not user-visible.

Kotlin

val request = GLSearchRequest(
GLSearchRequestType.Search,
"coffee",
MapGeoPoint(41.1579, -8.6291),
50,
arrayOf("en", "native"),
null,
)

requestID = request.startOnline(object : GLSearchRequest.ResultsCallback {
override fun onResult(objects: GLMapVectorObjectList) {
val nextResults = objects.toArray()
objects.close()
runOnUiThread { replaceResults(nextResults) }
}

override fun onError(error: GLMapError) {
runOnUiThread { showError(error.toString()) }
}
})

Use request.startOffline(callback) for downloaded maps. Android callbacks may arrive on a background thread or synchronously, so explicitly move UI work to the main thread.

toArray() returns independently owned objects. Close the result list immediately, and close every result object when replacing the table contents.

For Android table rows, GLSearch.GetDisplayInfo applies the same shared display rules used online and offline:

GLSearch.GetDisplayInfo(result, renderer.localeSettings)?.use { info ->
title.text = info.title?.string ?: "Unnamed"
subtitle.text = info.secondaryText?.string
}

The reference apps show a combined map and results table, autocomplete debouncing, cancellation, online/offline switching, markers, and ownership: