External Markers
This example demonstrates how to render custom markers using external rendering with the Maps SDK for TypeScript. It fetches data from the OpenChargeMap API and displays charging stations as interactive HTML markers.
Live Demo
Overview
The example demonstrates the following features:
- Creating custom HTML markers using
ExternalRendererMarkers - Fetching GeoJSON data from an external API
- Adding marker collections to the map
- Managing marker visibility and position updates
- Custom pin rendering with DOM elements
Code Implementation
Initialize GemKit and Setup
Fetch and Convert Data to GeoJSON (API Helper)
Add Markers to Map
Pin Manager Class
The PinManager class handles the creation, positioning, and lifecycle of HTML marker elements.
Key Features
- External Rendering: Custom HTML markers rendered outside the map canvas using
ExternalRendererMarkers - GeoJSON Support: Convert API data to GeoJSON format for marker collections
- Dynamic Updates: Markers automatically update position as the map moves
- Visibility Management: Only visible markers are rendered for optimal performance
- Custom Styling: Full control over marker appearance using CSS and DOM manipulation
How It Works
- Initialize External Renderer: Create an
ExternalRendererMarkersinstance - Fetch Data: Retrieve location data from an external API
- Convert to GeoJSON: Transform the data into GeoJSON format
- Add Marker Collection: Use
addGeoJsonAsMarkerCollection()to add markers to the map - Setup Notifications: Register a callback to update marker positions when the map changes
- Render Pins: The
PinManagercreates and positions HTML elements for each marker
Coordinate Conversion
Screen coordinates from the SDK are normalized (0-1 range) and need to be converted to pixel coordinates:
const screenCoordinates = {
x: normalizedX * mapWidth,
y: normalizedY * mapHeight
};
Customization Options
You can customize markers by modifying the createPinElement() method:
- Change size, color, and shape
- Add icons or images
- Include labels or badges
- Implement hover effects
- Add click handlers for interactions
Next Steps
- Custom Position Icon - Customize the position marker
- Map Selection - Handle marker selection