SearchstaxLocationWidget Angular
SearchStax Site Search solution offers a Angular search-location widget to assist with your custom search page.
The SearchstaxLocationWidget provides a location search input with location-based search functionality.
Usage
const config = {
appId: "APP_ID",
relatedSearchesAPIKey: "KEY"
}
const locationWidgetConfig = {
locationDecode: (term: string): Promise<ISearchstaxLocation> => {
return new Promise((resolve) => {
// make a request to google geocoding API to retrieve lat, lon and address
const geocodingAPIKey = "GEOCODING_API_KEY";
const geocodingURL = `https://maps.googleapis.com/maps/api/geocode/json?address=${encodeURIComponent(
term
)}&key=${geocodingAPIKey}`;
fetch(geocodingURL)
.then((response) => response.json())
.then((data) => {
if (data.status === "OK" && data.results.length > 0) {
const result = data.results[0];
const location = {
lat: result.geometry.location.lat,
lon: result.geometry.location.lng,
address: result.formatted_address,
};
resolve(location);
} else {
resolve({
address: undefined,
lat: undefined,
lon: undefined,
error: true,
});
}
})
.catch(() => {
resolve({
address: undefined,
lat: undefined,
lon: undefined,
error: true,
});
});
});
},
locationDecodeCoordinatesToAddress: (lat: string, lon: string): Promise<ISearchstaxLocation> => {
return new Promise((resolve) => {
fetch(
`https://geocoding-staging.searchstax.co/reverse?location=${lat},${lon}&components=country:US&app_id=${config.appId}`,
{
method: "GET",
headers: {
Authorization: `Token ${config.relatedSearchesAPIKey}`,
},
}
)
.then((response) => response.json())
.then((data) => {
if (data.status === "OK" && data.results.length > 0) {
const result = data.results[0];
resolve({
address: result.formatted_address,
lat: lat,
lon: lon,
error: false,
});
} else {
resolve({
address: undefined,
lat: lat,
lon: lon,
error: true,
});
}
})
.catch(() => {
resolve({
address: undefined,
lat: lat,
lon: lon,
error: true,
});
});
});
},
locationSearchEnabled: false,
locationValuesOverride: {
locationDistanceEnabled: true,
filterValues: ["any", "1000"],
filterUnit: "miles",
locationFilterDefaultValue: "any"
},
}
<app-searchstax-search-location
[locationDecode]="locationWidgetConfig.locationDecodeFunction"
[locationDecodeCoordinatesToAddress]="locationWidgetConfig.locationDecodeCoordinatesToAddress"
[locationValuesOverride]="locationWidgetConfig.locationValuesOverride"
[locationSearchEnabled]="locationWidgetConfig.locationSearchEnabled"
>
</app-searchstax-search-location>Props
locationDecode– callback function invoked after location input blurs and is used to decode string to coordinates.locationDecodeCoordinatesToAddress– callback function to override location decoding.locationValuesOverride– location widget valueslocationSearchEnabled– boolean stating if locationSearch is locationSearchEnabledtemplateOverride– template override. look at examples below
Example
For a full example, see searchstax-ux-angular.
Questions?
Do not hesitate to contact the SearchStax Support Desk.