Text Search
This example demonstrates how to perform text-based searches for landmarks and display results on an interactive map.
Live Demo
Overview
This example shows how to:
- Search for landmarks using text input
- Display search results with icons and details
- Highlight selected landmarks on the map
- Center the map on selected search results
Code Implementation
Imports
First, import the required modules from the SDK:
import {
GemKit,
GemMap,
Coordinates,
Landmark,
HighlightRenderSettings,
SearchService,
SearchPreferences,
GemError,
AddressField,
ImageFileFormat,
} from '@magiclane/maps-sdk';
Setup UI and Search Modal
Create the app bar with a search button and initialize the map:
Open Search Modal
Get the map center coordinates and open the search modal:
Perform Search
Execute the search using SearchService with preferences:
Display Search Results
Render the search results with icons and details:
Select Search Result
Handle result selection by highlighting and centering on the map:
Helper Functions
Extract address and distance information from landmarks:
Key Features
Search Service Integration
The example uses SearchService.search() with:
- Text Filter: User-entered search query
- Reference Coordinates: Map center coordinates for distance calculation
- Search Preferences: Configure max results (40), enable address and POI search
- Callback Handler: Process results or errors asynchronously
Search Preferences
Configure search behavior with SearchPreferences.create():
- maxMatches: Limit to 40 results
- searchAddresses: Include address results
- searchMapPOIs: Include Points of Interest
Result Display
Each search result shows:
- Icon: Landmark image (40x40px) or fallback emoji (📍)
- Name: Landmark name with ellipsis overflow
- Details: Distance in kilometers + full address (street, city, country)
- Hover Effect: Background color change on mouse over
Map Interaction
When a result is selected:
- Highlight:
map.activateHighlight([landmark])with render settings - Center:
map.centerOnCoordinates(landmark.coordinates, { zoomLevel: 70 }) - Feedback: Show message with selected landmark name
- Cleanup: Close modal and clear results
Implementation Details
- Screen to Coordinates:
map.transformScreenToWgs()converts screen center to geographic coordinates - Modal Management: Single modal instance, removed and recreated on each search
- Image Handling: Try to get landmark image, fallback to emoji if unavailable
- Error Handling: Check
err !== GemError.successbefore processing results - Distance Calculation: Extract from
landmark.extraInfo.getByKey('gmSearchResultDistance') - Address Extraction: Use
AddressFieldenum to get street, city, country
Use Cases
- Location Search: Find addresses, POIs, businesses by name
- Nearby Discovery: Search relative to current map view
- Quick Navigation: Jump to searched locations with single click
- Place Identification: View details and distance for each result
Next Steps
- Address Search - Search by structured address components
- Search by Location - Search within specific areas
- Category Search - Filter by POI categories