Skip to content

Commit e3eef17

Browse files
committed
Add ignore map events example and demo to README
1 parent 56173dc commit e3eef17

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed

README.md

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ React wrapper for mapbox-gl-geocoder for use with react-map-gl.
55
[![NPM](https://img.shields.io/npm/v/react-map-gl-geocoder.svg)](https://www.npmjs.com/package/react-map-gl-geocoder)
66

77

8-
## Demo
9-
https://codesandbox.io/s/l7p179qr6m
8+
## Demos
9+
* Simple Example - https://codesandbox.io/s/l7p179qr6m
10+
* Ignore Map Events Example - https://codesandbox.io/s/react-map-gl-geocoder-using-containerref-to-ignore-events-rewdh
1011

1112
## Installation
1213
npm
@@ -74,7 +75,9 @@ Only `mapRef` and `mapboxApiAccessToken` are required.
7475

7576

7677

77-
## Example
78+
## Examples
79+
80+
### Simple Example
7881
```js
7982
import 'mapbox-gl/dist/mapbox-gl.css'
8083
import 'react-map-gl-geocoder/dist/mapbox-gl-geocoder.css'
@@ -107,7 +110,7 @@ const Example = () => {
107110
...geocoderDefaultOverrides
108111
});
109112
},
110-
[handleViewportChange]
113+
[]
111114
);
112115

113116
return (
@@ -132,7 +135,58 @@ const Example = () => {
132135
};
133136

134137
export default Example
138+
```
139+
140+
### Ignore Map Events Example
141+
You can use the `containerRef` prop to place the `Geocoder` component outside of the `MapGL` component to avoid propagating the mouse events to the `MapGL` component. You can use CSS to position it over the map as shown in this example.
142+
```js
143+
import 'mapbox-gl/dist/mapbox-gl.css'
144+
import 'react-map-gl-geocoder/dist/mapbox-gl-geocoder.css'
145+
import React, { useState, useRef, useCallback } from 'react'
146+
import MapGL from 'react-map-gl'
147+
import Geocoder from 'react-map-gl-geocoder'
148+
149+
// Ways to set Mapbox token: https://uber.github.io/react-map-gl/#/Documentation/getting-started/about-mapbox-tokens
150+
const MAPBOX_TOKEN = 'REPLACE_WITH_YOUR_MAPBOX_TOKEN'
135151

152+
const Example = () => {
153+
const [viewport, setViewport] = useState({
154+
latitude: 37.7577,
155+
longitude: -122.4376,
156+
zoom: 8,
157+
});
158+
const geocoderContainerRef = useRef();
159+
const mapRef = useRef();
160+
const handleViewportChange = useCallback(
161+
(newViewport) => setViewport(newViewport),
162+
[]
163+
);
164+
165+
return (
166+
<div style={{ height: "100vh" }}>
167+
<div
168+
ref={geocoderContainerRef}
169+
style={{ position: "absolute", top: 20, left: 20, zIndex: 1 }}
170+
/>
171+
<MapGL
172+
ref={mapRef}
173+
{...viewport}
174+
width="100%"
175+
height="100%"
176+
onViewportChange={handleViewportChange}
177+
mapboxApiAccessToken={MAPBOX_TOKEN}
178+
>
179+
<Geocoder
180+
mapRef={mapRef}
181+
containerRef={geocoderContainerRef}
182+
onViewportChange={handleViewportChange}
183+
mapboxApiAccessToken={MAPBOX_TOKEN}
184+
position="top-left"
185+
/>
186+
</MapGL>
187+
</div>
188+
);
189+
};
136190
```
137191

138192
![react-map-gl-geocoder example screenshot](react-map-gl-geocoder-screenshot.png)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-map-gl-geocoder",
3-
"version": "2.0.15",
3+
"version": "2.0.16",
44
"description": "React wrapper for mapbox-gl-geocoder for use with react-map-gl",
55
"source": "src/index.js",
66
"main": "dist/index.js",

0 commit comments

Comments
 (0)