This directory contains the events.js file where the Smart Field Location setteris declared.
This smart field will be used to update the value of the addressand locationGeo fields.
/forest/events.js
constalgoliasearch=require('algoliasearch');constplaces=algoliasearch.initPlaces(process.env.PLACES_APP_ID,process.env.PLACES_API_KEY);asyncfunctiongetLocationCoordinates(query) {try {constlocation=awaitplaces.search({ query, type:'address' });console.log('search location coordinates result',location.hits[0]._geoloc);returnlocation.hits[0]._geoloc; } catch (err) {console.log(err);returnnull; }}asyncfunctionsetEvent(event, query) {constcoordinates=awaitgetLocationCoordinates(query);event.address = query;event.locationGeo =`{"type": "Point", "coordinates": [${coordinates.lat}, ${coordinates.lng}]}`;console.log('new address',event.address);console.log('new location',event.locationGeo);return event;}collection('events', { fields: [{ field:'Location setter', type:'String',// Get the data to be displayed.get: (event) =>event.address,// Update using Algolia.set: (event, query) =>setEvent(event, query), }],});
The field Location setter should use the address edit widget to enable address autocomplete.