You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Define the expected inputs for the scriptScript.Inputs={Query: "string",};// Define the expected outputs for the scriptScript.Outputs={Items: "array",Searched: "signal",};// Set the endpoint URL for the Mapbox geocoding APIconstENDPOINT='https://api.mapbox.com/geocoding/v5/mapbox.places';// Define an asynchronous function to make the API requestasyncfunctionmakeRequest(){// Get the Mapbox access token from Noodl project settingsconstaccess_token=Noodl.getProjectSettings().mapboxAccessToken;// Encode the search query to be URL-safeconstsearch_text=encodeURIComponent(Script.Inputs.Query);// Define query parameters for the API request// // Playground by Mapbox to test out all the features:// https://docs.mapbox.com/playground/geocoding//// Here is a list of all the different possible types:// - address// - country// - region// - postcode// - district// - place// - neighborhood// - locality// - poiconstqueryParams={
access_token,// Provide the access tokenproximity: [Script.Inputs.lng,Script.Inputs.lat].join(','),// Bias results towards user's locationlimit: 5,// Maximum number of results to return// types: ["address"].join(","), // Filter results to include only addresses// fuzzyMatch: true // Enable approximate matchinglanguage: 'en-GB'};// Construct the query string from the query parametersconstquery=Object.keys(queryParams).filter((key)=>!!queryParams[key])// Filter out empty values.map((key)=>`${encodeURIComponent(key)}=${encodeURIComponent(queryParams[key])}`).join('&');// Make the API request and get the response as JSONconstresponse=awaitfetch(`${ENDPOINT}/${search_text}.json?${query}`);constjson=awaitresponse.json();// Map the API response to an array of search resultsconstitems=json.features.map((x)=>Noodl.Object.create({text: x.text,place_name: x.place_name,// Convert the array of [latitude, longitude] to a Geopointcenter: {latitude: x.center[0],longitude: x.center[1]}}));Script.Outputs.Items=items;Script.Outputs.Searched();}Script.Signals={Search(){makeRequest();},};
Center to the clicked item:
constitems=Inputs.Items;constitemId=Inputs.ItemId;constitem=items.find((x)=>x.id===itemId);if(!item)thrownewError("Cannot find clicked item.");// The center geopoint of the clicked item.constgeopoint=item.center;constmapboxObject=Inputs.MapboxObject;if(!mapboxObject)thrownewError("Mapbox Object is invalid.");mapboxObject.flyTo({center: [geopoint.latitude,geopoint.longitude],essential: true,zoom: 15});
The repeater item is just a button that is outputting a Click signal.
The text was updated successfully, but these errors were encountered:
Improve documentation
Link
https://docs.noodl.net/2.9/library/modules/mapbox/guides/geocoder/
Describe the improvement
We should create a page descripting how to create a custom Geocoder / Address search component.
Additional context
Here is an example:
https://mapbox-module-test.sandbox.noodl.app/geocoding-api
Search:
Center to the clicked item:
The repeater item is just a button that is outputting a
Click
signal.The text was updated successfully, but these errors were encountered: