The following POI information search capabilities are provided:
Keyword search: Returns a list of places based on keywords entered by the user.
Nearby place search: Searches for nearby places based on the current location of the user's device.
Autocomplete: Returns an autocomplete place and a list of suggested places based on the entered keyword.
Place details search: Searches for details about a place.
The following table describes APIs for POI search, which are mainly provided by methods in the site namespace. For more information about the APIs, please refer to API Reference.
| API | Description |
|---|---|
| searchByText(searchByTextParams: SearchByTextParams): Promise<SearchByTextResult> | Performs keyword search. |
| searchByText(context: common.Context, searchByTextParams: SearchByTextParams): Promise<SearchByTextResult> | Performs keyword search. The context can be passed. |
| nearbySearch(nearbySearchParams: NearbySearchParams): Promise<NearbySearchResult> | Performs nearby place search. |
| nearbySearch(context: common.Context, nearbySearchParams: NearbySearchParams): Promise<NearbySearchResult> | Performs nearby place search. The context can be passed. |
| queryAutoComplete(queryAutoCompleteParams: QueryAutoCompleteParams): Promise<QueryAutoCompleteResult> | Performs autocomplete. |
| queryAutoComplete(context: common.Context, queryAutoCompleteParams: QueryAutoCompleteParams): Promise<QueryAutoCompleteResult> | Performs autocomplete. The context can be passed. |
| searchById(searchByIdParams: SearchByIdParams): Promise<SearchByIdResult> | Searches for place details. |
| searchById(context: common.Context, searchByIdParams: SearchByIdParams): Promise<SearchByIdResult> | Searches for place details. The context can be passed. |
Import required modules.
import { site } from '@kit.MapKit';
import { BusinessError } from '@kit.BasicServicesKit'; This capability allows a user to search for a place, such as a scenic spot, enterprise, or school, based on the user's input keyword and coordinate bounds. Coordinate bounds are optional.
let params: site.SearchByTextParams = {
// Set a search keyword, for example, "The Palace Museum" or "Confucius Temple".
query: 'Piazzale Dante, 41, 55049 Viareggio, Tuscany, Italy',
// Longitude-latitude coordinates.
location: {
latitude: 31.984,
longitude: 118.76625
},
// Specify the radius of a geographical location.
radius: 10000,
// Language of the search result.
language: 'en'
};
// Return the keyword search result.
try {
const result = await site.searchByText(params);
console.info(`Succeeded in searching by text. result is ${JSON.stringify(result)}`);
} catch (error) {
const err: BusinessError = error as BusinessError;
console.error(`Failed in searching by text. Code is ${err.code}, message is ${err.message}`);
} This capability searches for nearby places based on the user device location. You can optimize the search result by providing keywords or specifying the type of the places to search.
let params: site.NearbySearchParams = {
location: {
latitude: 51.50811219132287,
longitude: -0.07594896472392065
},
poiTypes: [
'Watch_Store',
'SUBWAY',
'PRIMARY_SCHOOL',
'GENERAL_AUTO_REPAIR_SERVICE_CENTER'
]
}
// Return the nearby place search result.
try {
const result = await site.nearbySearch(params);
console.info(`Succeeded in searching nearby. result is ${JSON.stringify(result)}`);
} catch (error) {
const err: BusinessError = error as BusinessError;
console.error(`Failed in searching nearby. Code is ${err.code}, message is ${err.message}`);
} This capability presents the most possible search terms to users based on their input keywords to reduce the amount of information they need to enter and improve their experience. For example, if a user enters Beijing, this capability suggests Beijing, Beijing Railway Station, and Beijing West Railway Station.
let params: site.QueryAutoCompleteParams = {
// Custom keyword.
query: 'hotel',
// Longitude-latitude coordinates.
location: {
latitude: 31.984410259206815,
longitude: 118.76625379397866
},
language: 'en',
// Return child nodes.
isChildren: true
};
// Return the autocomplete result.
try {
const result = await site.queryAutoComplete(params);
console.info(`Succeeded in querying. result is ${JSON.stringify(result)}`);
} catch (error) {
const err: BusinessError = error as BusinessError;
console.error(`Failed in querying. Code is ${err.code}, message is ${err.message}`);
} This capability obtains details about a place based on its unique site ID (siteId) which serves as the primary key. The details include the name, address, and longitude and latitude of the place. The value of siteId can be obtained from the return result of APIs of other capabilities including keyword search, nearby place search, place details search, autocomplete, and forward geocoding.
let params: site.SearchByIdParams = {
// Specify the primary key site ID.
siteId: '144129739873977856',
language: 'en',
// Return child nodes.
isChildren: true
};
// Return the place details search result.
try {
const result = await site.searchById(params);
console.info(`Succeeded in searching. result is ${JSON.stringify(result)}`);
} catch (error) {
const err: BusinessError = error as BusinessError;
console.error(`Failed in searching. Code is ${err.code}, message is ${err.message}`);
}