@@ -5,8 +5,9 @@ React wrapper for mapbox-gl-geocoder for use with react-map-gl.
5
5
[ ![ NPM] ( https://img.shields.io/npm/v/react-map-gl-geocoder.svg )] ( https://www.npmjs.com/package/react-map-gl-geocoder )
6
6
7
7
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
10
11
11
12
## Installation
12
13
npm
@@ -74,7 +75,9 @@ Only `mapRef` and `mapboxApiAccessToken` are required.
74
75
75
76
76
77
77
- ## Example
78
+ ## Examples
79
+
80
+ ### Simple Example
78
81
``` js
79
82
import ' mapbox-gl/dist/mapbox-gl.css'
80
83
import ' react-map-gl-geocoder/dist/mapbox-gl-geocoder.css'
@@ -107,7 +110,7 @@ const Example = () => {
107
110
... geocoderDefaultOverrides
108
111
});
109
112
},
110
- [handleViewportChange ]
113
+ []
111
114
);
112
115
113
116
return (
@@ -132,7 +135,58 @@ const Example = () => {
132
135
};
133
136
134
137
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'
135
151
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
+ };
136
190
```
137
191
138
192
![ react-map-gl-geocoder example screenshot] ( react-map-gl-geocoder-screenshot.png )
0 commit comments