Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PubNub Geocoding and Mapping w/ MapBox BLOCK
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