Skip to content

Commit 3d25a03

Browse files
author
Savina Shen (Manpower Services Taiwan Co Ltd)
committed
add map events example
1 parent 83d5e8a commit 3d25a03

File tree

5 files changed

+79
-1
lines changed

5 files changed

+79
-1
lines changed

Events/MapEvents.mdx

Whitespace-only changes.

Events/MapEvents.stories.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

Events/MapEvents.tsx

Whitespace-only changes.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import MapEvents from './MapEvents';
2+
import { Meta, Source } from '@storybook/addon-docs/blocks';
3+
4+
<Meta title="Events/Map Events"/>
5+
# Map Events
6+
7+
The Map component triggers events whenever the user interacts with the map.<br/>
8+
For a complete list of available events, refer to the [Map Events Sample](https://samples.azuremaps.com/map/map-events).
9+
10+
The example below shows how to listen for a click event.<br/>
11+
When the user clicks on the map, a popup will display the coordinates of the clicked location.
12+
13+
<MapEvents/>
14+
15+
<Source code={`
16+
import { AzureMap, AzureMapsProvider, AzureMapPopup } from 'react-azure-maps';
17+
import { useState } from 'react';
18+
19+
const MapEvents = () => {
20+
const [position, setPosition] = useState([0, 0]);
21+
22+
const handleMapClick = (e: any) => {
23+
setPosition(e.position);
24+
};
25+
const displayPosition = (position: number[]) => {
26+
// Format position as a string
27+
// round to 4 decimal places
28+
const longitude = \`\${Number(position[0]).toFixed(4)} °\${position[0] > 0 ? 'E' : 'W'}\`;
29+
const latitude = \`\${Number(position[1]).toFixed(4)} °\${position[1] > 0 ? 'N' : 'S'}\`;
30+
return \`\${longitude}, \${latitude}\`;
31+
};
32+
33+
return (
34+
<AzureMapsProvider>
35+
<AzureMap options={your_options} events={{ click: handleMapClick }}>
36+
<AzureMapPopup
37+
isVisible
38+
options={{ position: position }}
39+
popupContent={<div style={{ padding: '20px' }}>{displayPosition(position)}</div>}
40+
/>
41+
</AzureMap>
42+
</AzureMapsProvider>
43+
);
44+
};
45+
`}/>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { AzureMap, AzureMapsProvider, AzureMapPopup } from 'react-azure-maps';
2+
import { useState } from 'react';
3+
import { mapOptions } from '../../../key';
4+
5+
const MapEvents = () => {
6+
const [position, setPosition] = useState([0, 0]);
7+
8+
const handleMapClick = (e: any) => {
9+
setPosition(e.position);
10+
};
11+
const displayPosition = (position: number[]) => {
12+
// Format position as a string
13+
// round to 4 decimal places
14+
const longitude = `${Number(position[0]).toFixed(4)} °${position[0] > 0 ? 'E' : 'W'}`;
15+
const latitude = `${Number(position[1]).toFixed(4)} °${position[1] > 0 ? 'N' : 'S'}`;
16+
return `${longitude}, ${latitude}`;
17+
};
18+
19+
return (
20+
<div className="defaultMap sb-unstyled">
21+
<AzureMapsProvider>
22+
<AzureMap options={mapOptions} events={{ click: handleMapClick }}>
23+
<AzureMapPopup
24+
isVisible
25+
options={{ position: position }}
26+
popupContent={<div style={{ padding: '20px' }}>{displayPosition(position)}</div>}
27+
/>
28+
</AzureMap>
29+
</AzureMapsProvider>
30+
</div>
31+
);
32+
};
33+
34+
export default MapEvents;

0 commit comments

Comments
 (0)