Skip to content

Commit df4de2f

Browse files
authored
docs: Add documentation for accessing the map object in <MapView> (#1936)
* Add documentation for ref and useMap * Link to React's useRef hook
1 parent 344e65f commit df4de2f

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
```jsx{12,15,21}
2+
import { Button, MapView } from '@aws-amplify/ui-react';
3+
import { Amplify } from 'aws-amplify';
4+
import { useCallback, useRef } from 'react';
5+
6+
import '@aws-amplify/ui-react/styles.css';
7+
8+
import awsExports from './aws-exports';
9+
10+
Amplify.configure(awsExports);
11+
12+
export default function MapWithRef() {
13+
const mapRef = useRef();
14+
15+
const flyToMordor = useCallback(() => {
16+
mapRef.current.flyTo({ center: [172.78, -42.28], zoom: 5 });
17+
}, []);
18+
19+
return (
20+
<>
21+
<Button onClick={flyToMordor}>Fly, you fools!</Button>
22+
<MapView ref={mapRef} />
23+
</>
24+
);
25+
}
26+
```
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
```jsx
2+
import { Button, MapView } from '@aws-amplify/ui-react';
3+
import { Amplify } from 'aws-amplify';
4+
import { useMap } from 'react-map-gl';
5+
6+
import '@aws-amplify/ui-react/styles.css';
7+
8+
import awsExports from './aws-exports';
9+
10+
Amplify.configure(awsExports);
11+
12+
function FlyToButton() {
13+
const { current: map } = useMap();
14+
15+
const flyToMordor = () => {
16+
map.flyTo({ center: [172.78, -42.28], zoom: 5 });
17+
};
18+
19+
return <Button onClick={flyToMordor}>Fly, you fools!</Button>;
20+
}
21+
22+
export default function MapWithButton() {
23+
return (
24+
<MapView>
25+
<FlyToButton />
26+
</MapView>
27+
);
28+
}
29+
```

docs/src/pages/[platform]/components/geo/react.mdx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,20 @@ when the marker is clicked. The `offset` property can be used to prevent the pop
6161
_Note: Ensure `originalEvent.stopPropagation()` is called in the marker click handler. This allows the `showPopup`
6262
state to be handled by the component._
6363

64+
### Animation and Native Map Methods
65+
66+
You may want to access the [native maplibre-gl map object](https://maplibre.org/maplibre-gl-js-docs/api/map/) from within the component that renders `<MapView>` (to animate a viewport transition with `flyTo`, for example). To accomplish this, you can pass your own ref to `<MapView>` using [React's `useRef` hook](https://reactjs.org/docs/hooks-reference.html#useref) which will contain the `map` object:
67+
68+
<Fragment platforms={['react']}>
69+
{({ platform }) => import(`./fragments/map-ref.${platform}.mdx`)}
70+
</Fragment>
71+
72+
If you want access to the `map` object in a child component of `<MapView>`, you can use the [useMap hook from react-map-gl](https://visgl.github.io/react-map-gl/docs/api-reference/use-map) instead:
73+
74+
<Fragment platforms={['react']}>
75+
{({ platform }) => import(`./fragments/use-map.${platform}.mdx`)}
76+
</Fragment>
77+
6478
## Location Search
6579

6680
_Component can also be imported as `Geocoder`_

0 commit comments

Comments
 (0)