Search Category
This example demonstrates how to search for landmarks filtered by specific categories (e.g., restaurants, hotels, gas stations).
Live Demo
Overview
This example shows how to:
- Search for landmarks based on specific categories
- Display a multi-select category filter UI
- Combine text search with category filtering
- Highlight and navigate to selected search results
Code Implementation
Imports
First, import the required modules from the SDK:
import {
GemKit,
GemMap,
Coordinates,
PositionService,
Landmark,
LandmarkCategory,
SearchPreferences,
SearchService,
GemError,
HighlightRenderSettings,
HighlightOptions,
AddressField,
GemIcon,
} from '@magiclane/maps-sdk';
SDK Initialization
Initialize the SDK and create a map view (UI omitted):
Create Category Selection Modal
Build a modal with category checkboxes and text input:
Perform Category-filtered Search (SDK-only)
Execute search using selected categories and preferences (UI omitted):
Display Search Results
Format distance and address information:
Select and Highlight Result
Handle result selection and map navigation:
Utility Functions
Display temporary messages:
Key Features
Category Filtering
The example uses LandmarkCategory objects to filter search results:
Category Selection:
- Display all available categories from
GenericCategories.categories - Allow multiple category selection
- Visual feedback for selected categories
Adding Categories to Search:
selectedCategories.forEach(cat => {
preferences.landmarks.addStoreCategoryId(cat.landmarkStoreId, cat.id);
});
Search Preferences
Configure search behavior:
Parameters:
maxMatches: 40- Limit results to 40 landmarksallowFuzzyResults: false- Exact matching onlysearchMapPOIs: true- Search points of interestsearchAddresses: false- Exclude address results
Text Filter
Combine category filtering with text search:
SearchService.searchAroundPosition({
position: coords,
preferences: preferences,
textFilter: text,
onCompleteCallback: callback
});
The textFilter parameter allows searching within the selected categories.
Map Center Coordinates
Get the center point of the current map view:
const x = container.offsetWidth / 2;
const y = container.offsetHeight / 2;
const coords = map.transformScreenToWgs({
x: Math.floor(x),
y: Math.floor(y)
});
This ensures search results are relevant to the visible area.
Multi-select UI
The category list supports multiple selections:
- Click to toggle category selection
- Visual indicator (gray background) for selected items
- State persists when modal reopens with results
Implementation Details
- Category State: Maintain
selectedCategoriesarray across modal reopens - Dynamic UI: Rebuild modal after each search to show results
- Error Handling: Show "No results found" message on search failure
- Result Display: Show distance and address for each landmark
- Map Integration: Highlight and center map on selected result
Use Cases
- Restaurant Search: Filter by "Restaurants" category
- Hotel Booking: Search hotels in a specific area
- Gas Stations: Find nearby fuel stations
- POI Discovery: Browse landmarks by type (museums, parks, etc.)
- Trip Planning: Combine categories (hotels + restaurants)
Next Steps
- What is Nearby - Find all POIs around current location
- Search Location - Search by coordinates
- Text Search - Free-text search for landmarks