Search Location
This example demonstrates how to search for landmarks around a specific geographic location using coordinates.
Live Demo
Overview
This example shows how to:
- Search for landmarks around specific latitude/longitude coordinates
- Display search results with distance and address information
- Highlight and center the map on selected results
- Use
SearchService.searchAroundPosition()for proximity-based search
Code Implementation
Imports
First, import the required modules from the SDK:
import {
GemKit,
GemMap,
Coordinates,
Landmark,
SearchPreferences,
SearchService,
GemError,
HighlightRenderSettings,
HighlightOptions,
AddressField,
} from '@magiclane/maps-sdk';
Setup UI and Map
Initialize the map and add a search button:
Create Search Sidebar
Build a sidebar with latitude/longitude inputs:
Perform Search Around Position
Execute the search using coordinates:
Display Search Results
Show results with distance and address:
Select and Highlight Result
Handle result selection:
Utility Functions
Display temporary messages:
Key Features
Search Around Position
The SearchService.searchAroundPosition() method searches for landmarks within proximity of given coordinates:
Parameters:
position:Coordinatesobject with latitude/longitudepreferences:SearchPreferenceswith max results and fuzzy search optionsonCompleteCallback: Callback receiving error code and results array
Search Preferences:
maxMatches: Limit to 40 resultsallowFuzzyResults: Enable approximate matching
Coordinate Input
The modal accepts:
- Latitude: Decimal degrees (-90 to 90)
- Longitude: Decimal degrees (-180 to 180)
- Validation: Parse and check for valid numeric values
Distance Calculation
Extract distance from landmark extra info:
const dist = landmark.extraInfo?.getByKey?.('gmSearchResultDistance') || 0;
const km = Math.round(dist / 1000);
The distance is in meters, converted to kilometers for display.
Address Extraction
Use AddressField enum to get structured address components:
AddressField.streetName- Street nameAddressField.city- City nameAddressField.country- Country name
Result Highlighting
Configure highlight options:
const renderSettings = new HighlightRenderSettings({
options: new Set([HighlightOptions.showLandmark])
});
This ensures the landmark is visible on the map.
Implementation Details
- Modal State: Single modal instance, recreated when results change
- Error Handling: Validate input coordinates before search
- Search Feedback: Show "No results found" or result count
- Auto-refresh: Reopen modal with results after successful search
- Cleanup: Clear results when modal is closed
Use Cases
- Proximity Search: Find nearby POIs from any location
- Coordinate-based Discovery: Explore areas by coordinates
- Travel Planning: Preview landmarks before visiting
- Remote Location Search: Search areas without current position
Next Steps
- Category Search - Filter by POI categories
- Text Search - Free-text search for landmarks
- Address Search - Search by structured address