Created
November 2, 2016 03:04
-
-
Save sunnygleason/42e6e32c1c53e8fc2f910f3b64f2a9b8 to your computer and use it in GitHub Desktop.
PubNub Geocoding and Mapping w/ MapBox 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
let xhr = require('xhr'); | |
let query = require('codec/query_string'); | |
export default request => { | |
let mapbox_clientToken = '000000000000000000000000000000000000000000000000000000000000000000000000'; | |
let mapbox_geo_apiUrl = 'https://api.mapbox.com/geocoding/v5/mapbox.places/'; | |
let mapbox_maps_apiUrl = 'https://api.mapbox.com/styles/v1/mapbox/streets-v8/static'; | |
let searchParam = request.message.query; | |
if (!query) { | |
return request.ok(); | |
} | |
let mapbox_queryParams = { | |
access_token: mapbox_clientToken, | |
}; | |
let geo_url = mapbox_geo_apiUrl + escape(searchParam) + '.json' + '?' + query.stringify(mapbox_queryParams); | |
let map_zoom = 9; | |
let map_resolution = '200x150'; | |
return xhr.fetch(geo_url) | |
.then((response) => { | |
return response.json() | |
.then((parsedResponse) => { | |
if (parsedResponse.features && parsedResponse.features[0]) { | |
let geo = parsedResponse.features[0]; | |
request.message.geocode = geo; | |
request.message.map = mapbox_maps_apiUrl += '/' + geo.center[0] + ',' + geo.center[1] + ',' + map_zoom + '/' + map_resolution + '?' + query.stringify(mapbox_queryParams); | |
} | |
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