Skip to content

Instantly share code, notes, and snippets.

@jgravois
Created March 1, 2017 21:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgravois/d127ce11b71150f81e9cae74879e0997 to your computer and use it in GitHub Desktop.
Save jgravois/d127ce11b71150f81e9cae74879e0997 to your computer and use it in GitHub Desktop.
PubNub / Esri geocoding block
// Test Payload
{
"text": "Disneyland"
}
// block
export default (request) => {
let xhr = require('xhr');
let query = require('codec/query_string');
// one off geocoding requests do not require authentication if the results are *not* stored
// https://developers.arcgis.com/rest/geocode/api-reference/geocoding-free-vs-paid.htm
let apiUrl = 'https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/findAddressCandidates';
let searchParam = request.message.text;
// return if the block does not have anything to analyze
if (!query) {
return request.ok();
}
let queryParams = {
singleLine: searchParam,
outFields: "*",
f: 'json'
};
let url = apiUrl + '?' + query.stringify(queryParams);
return xhr.fetch(url)
.then((response) => {
return response.json()
.then((parsedResponse) => {
request.message.geocode = parsedResponse;
console.log(request.message.geocode.candidates.length + ' candidates found');
return request;
})
.catch((err) => {
console.log('error happened on JSON parse', err);
return request;
});
})
.catch((err) => {
console.log('error happened for XHR.fetch', err);
return request;
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment