Created
March 1, 2017 21:06
-
-
Save jgravois/d127ce11b71150f81e9cae74879e0997 to your computer and use it in GitHub Desktop.
PubNub / Esri geocoding block
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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