Skip to content

Instantly share code, notes, and snippets.

@carlosrusso
Created May 10, 2017 13:13
Show Gist options
  • Save carlosrusso/efa9fef787a24563bebee96db5bde347 to your computer and use it in GitHub Desktop.
Save carlosrusso/efa9fef787a24563bebee96db5bde347 to your computer and use it in GitHub Desktop.
LocationResolver that uses "data science toolkit"
/*!
* Copyright 2017 Pentaho Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
define([
"cdf/AddIn",
"cdf/Dashboard.Clean",
"cdf/lib/jquery",
"amd!cdf/lib/underscore"
], function(AddIn, Dashboard, $, _) {
"use strict";
var addIn = {
name: "geocode",
label: "geocode",
defaults: {
url: "http://www.datasciencetoolkit.org/maps/api/geocode/json",
serviceParams: {
sensor: "false"
},
mapping: {
"street": "street",
"postalcode": "postalcode",
"city": "city",
"county": "county",
"state": "state",
"country": "country"
}
},
implementation: function(tgt, st, opt) {
if (st.latitude || st.longitude) {
var location = [
parseFloat(st.longitude),
parseFloat(st.latitude)
];
st.continuationFunction(location);
return;
}
var address = _.compact(_.map(opt.mapping, function(field) {
return st.data[st.mapping[field]];
})).join(", ");
var params = $.extend({}, opt.serviceParams, {
address: address
});
var onSuccess = function(data) {
var result = data.results;
if (result && result.length && result[0].geometry && result[0].geometry.location) {
var location = [
parseFloat(result[0].geometry.location.lng),
parseFloat(result[0].geometry.location.lat)
];
st.continuationFunction(location);
}
};
var onError = function() {
st.continuationFunction([]);
};
return $.ajax({
dataType: "jsonp",
method: "GET",
url: opt.url,
data: params,
success: onSuccess,
error: onError
});
}
};
Dashboard.registerGlobalAddIn("NewMapComponent", "LocationResolver", new AddIn(addIn));
return addIn;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment