Custom Position Icon
This example demonstrates how to customize the position tracker icon on the map using the Maps SDK for TypeScript. It shows how to load a custom PNG image to replace the default position marker and follow the user's position.
Live Demo
Overview
The example demonstrates the following features:
- Customizing the position tracker with a custom PNG image
- Setting scale and appearance of the position marker
- Requesting location permissions
- Following the user's position on the map
Code Implementation
Initialize GemKit and Setup
Load and Set Custom Position Icon
Request Location Permission and Follow Position
Utility Functions
Key Features
- Custom PNG Image: Replace the default position tracker with a custom PNG icon.
- Scalable Icons: Adjust the size of the position marker with the
scaleproperty. - Asset Loading: Load external PNG images from URLs.
- Position Tracking: Follow user's real-time location with smooth animations.
- Permission Handling: Automatic location permission request on web platforms.
Customization Options
Scale
Adjust the size of the position tracker:
positionTracker.scale = 0.7; // 70% of original size
positionTracker.scale = 2.0; // 200% of original size
Animation
Add smooth transitions when following position:
const animation = new GemAnimation({
type: AnimationType.linear,
duration: 1000 // milliseconds
});
map?.startFollowingPosition({ animation });
Position Tracker Properties
Access and modify the default position tracker:
const positionTracker = MapSceneObject.getDefPositionTracker();
positionTracker.scale = 1.5;
// Additional properties can be set here
Asset Preparation
When preparing custom position icons:
- Size: Keep file size small for faster loading (< 1MB recommended)
- Format: Use PNG for best compatibility and features
- Orientation: Ensure the image faces the correct direction
- Scale: Test different scale values to find the optimal size
- Location: Place assets in the public folder or serve from a CDN
Error Handling
The example includes error handling for asset loading:
Explanation of Key Components
- MapSceneObject.customizeDefPositionTracker: Sets the image for the default position tracker.
- SceneObjectFileFormat.tex: Specifies PNG image format for the position tracker.
- GemAnimation: Used for smooth camera movement when following position.
- PositionService.requestLocationPermission: Requests location permission from the user.
Next Steps
- Follow Position - Learn more about position tracking
- External Markers - Add custom markers to the map
- Map Gestures - Handle user interactions