Last active
March 29, 2017 22:24
-
-
Save andygup/37d2e3f45d91892cc661857089b01b0d to your computer and use it in GitHub Desktop.
A pubnub block for converting lat/lon to an address
This file contains hidden or 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
| /* | |
| This ArcGIS-based PubNub Block shows a pattern for converting a lat/lon coordinate to an address | |
| Declare the Event Handler with the export syntax. The incoming message is called request. | |
| Set up the test payload | |
| { | |
| "lat": -105.521, | |
| "lon": 40.377 | |
| } | |
| */ | |
| export default (request) => { | |
| let xhr = require('xhr'); | |
| let query = require('codec/query_string'); | |
| const console = require('console'); | |
| // 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/reverseGeocode'; | |
| let searchParam = request.message; | |
| // return if the block does not have anything to analyze | |
| if (!query) { | |
| return request.ok(); | |
| } | |
| let queryParams = { | |
| location: searchParam.lat + ", " + searchParam.lon, | |
| 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("Ouput: " + request.message.geocode.address.Address); | |
| 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