Address Search
This example demonstrates how to perform guided address searches and display the results on an interactive map.
Live Demo
Overview
This example shows how to:
- Search for addresses hierarchically (country → city → street → house number)
- Use the
GuidedAddressSearchServicefor structured address lookup - Highlight and center the map on the found address
- Display search progress messages
Code Implementation
Imports
First, import the required modules from the SDK:
import {
GemKit,
GemMap,
Landmark,
AddressDetailLevel,
GemError,
AnimationType,
GuidedAddressSearchService,
GemAnimation
} from '@magiclane/maps-sdk';
Setup UI and Map
Initialize the map and add a search button:
Address Search Helper
Create a reusable function to search for address components:
Hierarchical Address Search
Search through address hierarchy from country to house number:
Highlight and Center on Result
Display the found address on the map:
Utility Functions
Show temporary status messages:
function showMessage(message: string, duration = 3000) {
let msgDiv = document.getElementById('search-msg');
if (!msgDiv) {
msgDiv = document.createElement('div');
msgDiv.id = 'search-msg';
msgDiv.style.cssText = `
position: fixed; top: 20px; right: 20px; background: #333;
color: #fff; padding: 12px 20px; border-radius: 8px;
z-index: 2000; font-size: 1em;
`;
document.body.appendChild(msgDiv);
}
msgDiv.textContent = message;
setTimeout(() => {
msgDiv.textContent = '';
}, duration);
}
Key Features
Guided Address Search
The GuidedAddressSearchService enables hierarchical address lookup:
- Country Level: Start with
getCountryLevelItem('ESP')for Spain - City Level: Search within country with
AddressDetailLevel.city - Street Level: Search within city with
AddressDetailLevel.street - House Number: Find specific address with
AddressDetailLevel.houseNumber
Address Detail Levels
The AddressDetailLevel enum defines search granularity:
country- Country-level searchcity- City/town-level searchstreet- Street-level searchhouseNumber- Specific house number
Error Handling
The search callback returns two possible success codes:
GemError.success- Full results returnedGemError.reducedResult- Partial/reduced results available
Both are considered valid responses. Any other error code indicates failure.
Map Presentation
When displaying the result:
- Activate Highlight:
map.activateHighlight([landmark])marks the location - Center Map:
map.centerOnCoordinates()with zoom level 50 - Animation: Use
GemAnimationwithAnimationType.linearfor smooth transition
Implementation Details
- Async/Await Pattern: Use Promises to handle async search callbacks
- Early Returns: Exit search flow if any level fails to find results
- Console Logging: Log each successful level for debugging
- Status Messages: Show progress and completion messages to user
- Country Codes: Use ISO 3166-1 alpha-3 codes (e.g., 'ESP' for Spain)
Use Cases
- Complete Address Lookup: Find exact addresses from hierarchical components
- Address Validation: Verify address existence through each level
- Location Discovery: Guide users through address selection process
- International Addressing: Search addresses in any supported country
Next Steps
- Search by Location - Search within specific areas
- Category Search - Filter by POI categories
- Text Search - Free-text search for landmarks