Skip to content

Instantly share code, notes, and snippets.

@jelmervdl
Created February 6, 2018 14:56
Show Gist options
  • Save jelmervdl/7259cc07954993d73284dc6504b73ea3 to your computer and use it in GitHub Desktop.
Save jelmervdl/7259cc07954993d73284dc6504b73ea3 to your computer and use it in GitHub Desktop.
var PDOKGeocoder = L.Class.extend({
options: {
suggestUrl: 'https://geodata.nationaalgeoregister.nl/locatieserver/v3/suggest',
lookupUrl: 'https://geodata.nationaalgeoregister.nl/locatieserver/v3/lookup',
geocodingQueryParams: {
wt: 'json'
}
},
initialize: function(options) {
L.Util.setOptions(this, options);
},
geocode: function(value, callback, context) {
var query = L.Util.extend({}, this.options.geocodingQueryParams, {'q': value});
fetchJSON(this.options.suggestUrl + L.Util.getParamString(query), function(response) {
var results = response.response.docs.map(function(doc) {
return {
id: doc.id,
name: doc.weergavenaam,
};
});
callback.call(context, results);
});
},
lookup: function(id, callback, context) {
fetchJSON(this.options.lookupUrl + L.Util.getParamString({id: id}), function(response) {
var wkt = response.response.docs[0].centroide_ll;
var match = wkt.match(/POINT\((\d+(?:\.\d+)?)\s+(\d+(?:\.\d+)?)\)/);
callback.call(context, [parseFloat(match[2], 10), parseFloat(match[1], 10)]);
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment