Skip to content

Commit 6b01cdf

Browse files
authored
chore: added a location search google places migration doc (aws-amplify#4750)
* chore: added a location search google places migration doc * fix: removed unnecessary callout
1 parent a3c5034 commit 6b01cdf

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

src/fragments/lib/geo/js/google-migration.mdx

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,127 @@ marker.addListener('click', () => {
228228
</BlockSwitcher>
229229

230230
![A map with a marker popup](/images/map-marker-popup.png)
231+
232+
## Add a search component
233+
234+
Now we can try adding a search bar to your map which can return results and place markers on a map based on those results.
235+
236+
<BlockSwitcher>
237+
238+
<Block name="Amplify">
239+
240+
With Amplify Geo and MapLibre you can do the following.
241+
242+
```js
243+
const { createMap, createAmplifyGeocoder } = AmplifyMapLibre; // import from above updated to include createAmplifyGeocoder
244+
245+
const geocoder = createAmplifyGeocoder();
246+
map.addControl(geocoder);
247+
```
248+
249+
Save your changes and refresh your page and now when you should see a maplibre-gl-geocoder control in the top right corner of your map.
250+
251+
<Callout>
252+
253+
This example uses the [MapLibre's geocoder component](https://github.com/maplibre/maplibre-gl-geocoder) to create a search component. To see more options for our `createAmplifyGeocoder` utility function check out the docs [here](https://github.com/aws-amplify/maplibre-gl-js-amplify/blob/main/API.md#createAmplifyGeocoder).
254+
255+
</Callout>
256+
257+
</Block>
258+
259+
<Block name="Google Maps">
260+
261+
Using the Google Places JS API you would add a search bar as shown below.
262+
263+
```js
264+
// Create the search box and link it to the UI element.
265+
const input = document.getElementById("pac-input") as HTMLInputElement;
266+
const searchBox = new google.maps.places.SearchBox(input);
267+
268+
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
269+
270+
// Bias the SearchBox results towards current map's viewport.
271+
map.addListener("bounds_changed", () => {
272+
searchBox.setBounds(map.getBounds() as google.maps.LatLngBounds);
273+
});
274+
275+
// Listen for the event fired when the user selects a prediction and retrieve more details for that place.
276+
searchBox.addListener("places_changed", () => {
277+
const places = searchBox.getPlaces();
278+
279+
if (places.length == 0) {
280+
return;
281+
}
282+
283+
// For each place, get the icon, name and location.
284+
places.forEach((place) => {
285+
// Create markers for each place
286+
// Extend map view for each place
287+
});
288+
map.fitBounds(bounds);
289+
});
290+
```
291+
292+
</Block>
293+
294+
</BlockSwitcher>
295+
296+
![A search box](/images/geocoder-custom-images.png)
297+
298+
## Add a stand alone search component
299+
300+
Now we can try adding a search bar without adding it to a map which can return results that you can use.
301+
302+
<BlockSwitcher>
303+
304+
<Block name="Amplify">
305+
306+
With Amplify Geo and MapLibre you can do the following.
307+
308+
```js
309+
// Create a div to hold the search component
310+
const el = document.createElement("div");
311+
el.setAttribute("id", "search");
312+
document.body.appendChild(el);
313+
314+
// Create the geocoder component and append it to the div you created earlier
315+
const geocoder = createAmplifyGeocoder();
316+
document.getElementById("search").appendChild(geocoder.onAdd());
317+
```
318+
319+
Save your changes and refresh your page and now when you should see a maplibre-gl-geocoder control in the div you created.
320+
321+
<Callout>
322+
323+
This example uses the [MapLibre's geocoder component](https://github.com/maplibre/maplibre-gl-geocoder) to create a search component. To see more options for our `createAmplifyGeocoder` utility function check out the docs [here](https://github.com/aws-amplify/maplibre-gl-js-amplify/blob/main/API.md#createAmplifyGeocoder).
324+
325+
</Callout>
326+
327+
</Block>
328+
329+
<Block name="Google Maps">
330+
331+
Using the Google Places JS API you would add a stand alone search bar as shown below.
332+
333+
<Callout>
334+
335+
Some lines omitted for brevity, see the Google Maps Platform Places Search Box example for the full application
336+
337+
</Callout>
338+
339+
```js
340+
// Create a input to hold the search component
341+
const el = document.createElement("input");
342+
el.setAttribute("id", "pac-input");
343+
document.body.appendChild(el);
344+
345+
// Create the search box and link it to the UI element.
346+
const input = document.getElementById("pac-input");
347+
const searchBox = new google.maps.places.SearchBox(input);
348+
```
349+
350+
</Block>
351+
352+
</BlockSwitcher>
353+
354+
![A search box](/images/geocoder-search-box.png)

0 commit comments

Comments
 (0)