Location Wikipedia
This example demonstrates how to retrieve and display Wikipedia information for landmarks using the Maps SDK.
Live Demo
Overview
This example shows how to:
- Search for a specific landmark by name and coordinates
- Check if Wikipedia information is available for a landmark
- Request Wikipedia content including title, description, and images
- Display Wikipedia data in a modal interface
Code Implementation
Imports
First, import the required modules from the SDK:
import {
GemKit,
GemMap,
Coordinates,
SearchService,
ExternalInfoService,
ExternalInfo,
Landmark,
PositionService,
GemError
} from '@magiclane/maps-sdk';
Setup State Variables
Initialize global variables for UI elements:
let map: GemMap | null = null;
let wikiBtn: HTMLButtonElement;
let wikiModal: HTMLDivElement | null = null;
Initialize Map and UI
Setup the map view and Wikipedia button:
Search for Landmark
Search for the Statue of Liberty using text and coordinates:
Request Wikipedia Information
Fetch Wikipedia content for the landmark:
Display Wikipedia Modal
Show Wikipedia information in a modal dialog:
Utility Functions
Display temporary status messages:
Key Features
Check Wikipedia Availability
Before requesting Wikipedia data, verify it's available:
if (!ExternalInfoService.hasWikiInfo(lmk)) {
// Wikipedia info not available for this landmark
return;
}
This prevents unnecessary API calls for landmarks without Wikipedia entries.
Request Wikipedia Data
Use ExternalInfoService to fetch Wikipedia content:
Method:
ExternalInfoService.requestWikiInfo(
landmark,
(err: GemError, info: ExternalInfo | null) => {
// Handle response
}
);
Parameters:
landmark: TheLandmarkobject to get info forcallback: Receives error code andExternalInfoobject
Access Wikipedia Content
Extract Wikipedia data from ExternalInfo:
Properties:
externalInfo.wikiPageTitle- Wikipedia article titleexternalInfo.wikiPageDescription- Article content/descriptionexternalInfo.getWikiImageUrl(index)- Get image URL at index
Image Information:
externalInfo.requestWikiImageInfo(
0, // Image index
(error, imageInfo) => {
console.log("Image info:", imageInfo);
}
);
Search with Reference Coordinates
Improve search accuracy by providing reference coordinates:
SearchService.search({
textFilter: "Statue of Liberty",
referenceCoordinates: new Coordinates({
latitude: 40.53859,
longitude: -73.91619
}),
onCompleteCallback: callback
});
Reference coordinates help disambiguate landmarks with similar names.
Modal Display Pattern
The modal uses modern web design patterns:
Features:
- Semi-transparent overlay background
- Centered panel with max dimensions
- Scrollable content for long Wikipedia articles
- Click outside to close functionality
- Responsive design (90vw, 80vh)
Promise-based Async Pattern
Wrap SDK callbacks in Promises for cleaner async code:
const searchResult = await new Promise<Landmark[]>((resolve) => {
SearchService.search({
textFilter: text,
referenceCoordinates: coords,
onCompleteCallback: (err, lmks) => resolve(lmks)
});
});
This enables async/await syntax instead of nested callbacks.
Implementation Details
- Error Handling: Check for null results and missing Wikipedia data
- Image Display: Conditionally display images if available
- HTML Content: Use
innerHTMLto render formatted Wikipedia text - Modal Cleanup: Remove previous modal before creating new one
- Background Dismiss: Click outside modal to close
- Loading Feedback: Show "Searching..." message during API calls
Use Cases
- Tourist Information: Display Wikipedia info for landmarks and monuments
- Educational Apps: Provide historical context for locations
- Travel Guides: Show detailed descriptions of destinations
- Museum Apps: Display information about exhibits and artifacts
- City Tours: Provide background information for tour stops
- Cultural Apps: Share historical and cultural context
Next Steps
- Text Search - Free-text search for landmarks
- Search Location - Search around specific coordinates
- What is Nearby - Find all nearby POIs