@@ -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
1213npm
@@ -74,7 +75,9 @@ Only `mapRef` and `mapboxApiAccessToken` are required.
7475
7576
7677
77- ## Example
78+ ## Examples
79+
80+ ### Simple Example
7881``` js
7982import ' mapbox-gl/dist/mapbox-gl.css'
8083import ' 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
134137export 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 )
0 commit comments