Skip to content

Commit 51a2efd

Browse files
authored
[example] Animate symbol to follow mouse (#4945)
* [example] Animate symbol to follow mouse * cleanup * add @see to mousemove * add control
1 parent ef6e9ee commit 51a2efd

3 files changed

Lines changed: 75 additions & 0 deletions

File tree

408 KB
Loading

src/ui/events.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export type MapLayerEventType = {
7777
* @see [Get coordinates of the mouse pointer](https://maplibre.org/maplibre-gl-js/docs/examples/mouse-position/)
7878
* @see [Highlight features under the mouse pointer](https://maplibre.org/maplibre-gl-js/docs/examples/hover-styles/)
7979
* @see [Display a popup on over](https://maplibre.org/maplibre-gl-js/docs/examples/popup-on-hover/)
80+
* @see [Animate symbol to follow the mouse](https://maplibre.org/maplibre-gl-js/docs/examples/animate-symbol-to-follow-mouse/)
8081
*/
8182
mousemove: MapLayerMouseEvent;
8283
/**
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<title>Animate symbol to follow the mouse</title>
6+
<meta property="og:description" content="Animate symbol to follow the mouse." />
7+
<meta charset='utf-8'>
8+
<meta name="viewport" content="width=device-width, initial-scale=1">
9+
<link rel='stylesheet' href='../../dist/maplibre-gl.css' />
10+
<script src='../../dist/maplibre-gl-dev.js'></script>
11+
<style>
12+
body {
13+
margin: 0;
14+
padding: 0;
15+
}
16+
17+
html,
18+
body,
19+
#map {
20+
height: 100%;
21+
}
22+
</style>
23+
</head>
24+
25+
<body>
26+
<div id="map"></div>
27+
<script>
28+
const map = new maplibregl.Map({
29+
container: 'map',
30+
projection: {type: 'globe'},
31+
style:
32+
'https://api.maptiler.com/maps/streets/style.json?key=get_your_own_OpIi9ZULNHzrESv6T2vL',
33+
center: [0, 0],
34+
zoom: 2
35+
});
36+
37+
map.on('mousemove', (e) => {
38+
const lngLat = e.lngLat.wrap();
39+
const pointSource = map.getSource('point');
40+
41+
if (pointSource && lngLat.lng && lngLat.lat) {
42+
pointSource.setData({
43+
'type': 'Point',
44+
'coordinates': [lngLat.lng, lngLat.lat]
45+
});
46+
}
47+
});
48+
map.addControl(new maplibregl.GlobeControl());
49+
50+
map.on('load', () => {
51+
52+
map.addSource('point', {
53+
'type': 'geojson',
54+
'data': {
55+
'type': 'Point',
56+
'coordinates': [0, 0]
57+
}
58+
});
59+
60+
map.addLayer({
61+
'id': 'point',
62+
'source': 'point',
63+
'type': 'symbol',
64+
'layout': {
65+
'icon-image': 'airport_15',
66+
'icon-allow-overlap': true,
67+
'icon-ignore-placement': true
68+
},
69+
});
70+
});
71+
</script>
72+
</body>
73+
74+
</html>

0 commit comments

Comments
 (0)