Display Cursor Street Name
This example demonstrates how to display the name of the street at the cursor position when users tap on the map.
Live Demo
Overview
This example shows how to:
- Enable cursor rendering on the map
- Register touch callbacks to detect tap positions
- Set cursor position based on screen coordinates
- Retrieve street information at the cursor location
- Display street names in a UI overlay
Code Implementation
Imports
First, import the required modules from the SDK:
import {
GemKit,
GemMap,
Coordinates,
PositionService
} from '@magiclane/maps-sdk';
Setup State Variables
Initialize global variables for map and UI elements:
let map: GemMap | null = null;
let currentStreetName = "";
// UI Elements
let streetNameDiv: HTMLDivElement;
Initialize Map with Cursor
Setup the map centered on Milan, Italy with cursor enabled:
Initialize GemKit and Map View
Setup the map container and initialize the SDK:
Display Street Name UI
Create and update the street name overlay:
Utility Functions
Display temporary status messages:
Key Features
Enable Cursor
Configure map preferences to enable cursor functionality:
map.preferences.enableCursor = true;
map.preferences.enableCursorRender = true;
Properties:
enableCursor- Enable cursor detection and selectionenableCursorRender- Render cursor visually on map
Touch Callback Registration
Register a callback to handle tap events:
map.registerTouchCallback(async (point: any) => {
// Handle touch event
});
Callback Parameter:
point- Screen coordinates{x, y}where user tapped
Set Cursor Position
Update cursor to tapped screen position:
await map.setCursorScreenPosition(point);
This method:
- Sets the cursor to the specified screen coordinates
- Triggers internal selection logic
- Enables retrieval of street information
Retrieve Street Information
Get streets at cursor position:
const streets = map.cursorSelectionStreets();
Return Value:
- Array of street objects at cursor location
- Each street has a
nameproperty - Empty array if no streets found
Handle Unnamed Streets
Provide fallback for streets without names:
currentStreetName = (streets && streets.length > 0)
? (streets[0].name || "Unnamed street")
: "Unnamed street";
This ensures the UI always displays meaningful text.
Dynamic UI Display
Show/hide street name based on availability:
streetNameDiv.style.display = name ? 'block' : 'none';
The overlay only appears when a street name is available.
Bottom-Centered Overlay
Position the street name at the bottom center:
position: fixed;
bottom: 25px;
left: 50%;
transform: translateX(-50%);
This creates a clear, non-intrusive display area.
Implementation Details
- Lazy Initialization: Street name div created on first use
- Async Operations:
setCursorScreenPositionis awaited before retrieving streets - Null Checking: Verify
streetsarray exists and has elements - Fallback Text: Display "Unnamed street" for unnamed or missing streets
- Visual Design: White background with shadow for readability
- Z-index: 2000 ensures overlay appears above map
- Centering: Transform translate for perfect horizontal centering
Use Cases
- Navigation Apps: Show current street name during navigation
- Real Estate Apps: Display property street information
- Delivery Apps: Confirm delivery address street
- City Exploration: Learn street names while browsing the map
- Address Selection: Help users confirm selected location
- Educational Apps: Teach geography and street layouts
- Tourist Apps: Identify street names in foreign cities
Advanced Features
Multiple Streets
Handle intersections with multiple streets:
if (streets && streets.length > 1) {
const streetNames = streets.map(s => s.name).join(" / ");
updateStreetNameUI(streetNames);
}
Street Details
Access additional street properties:
streets.forEach(street => {
console.log('Street:', street.name);
console.log('Type:', street.type);
// Additional properties may be available
});
Custom Styling
Customize the overlay appearance:
streetNameDiv.style.cssText = `
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: #fff;
font-weight: bold;
border: 2px solid #fff;
`;
Next Steps
- Text Search - Search for landmarks by text
- Location Wikipedia - Get Wikipedia info for locations
- Search Location - Search around coordinates