Source: searchers/addressSearcher.js

 /** 
 *  
 * @class Searches smartAdresse (https://smartadresse.dk/)
 * @extends Septima.Search.Searcher
 * @constructs Septima.Search.AddressSearcher
 * @param {Object} options AddressSearcher expects these properties:
 * @param options.apiKey {string} eg.; "FCF3FC50-C9F6-4D89-9D7E-6E3706C1A0BD"
 * @param options.area {string} "muncode" + municipalitycode eg.; "muncode0151"
 *  
 * @example
 * var adressSearchOptions = {
 *     apiKey : "FCF3FC50-C9F6-4D89-9D7E-6E3706C1A0BD",
 *     onSelect : function(result) {
 *          jQuery('#log').prepend('<b>Plan valgt:</b> '+ result.title);},
 *     area: "muncode0151",
 *     matchesPhrase: "Begins with ",
 *     searchDelay: 200
 * };
 * searchers.push({title : 'Virksomhed', searcher : new Septima.Search.AddressSearcher(adressSearchOptions)});
 *
 */
Septima.Search.AddressSearcher = Septima.Class (Septima.Search.Searcher, /**  @lends Septima.Search.AddressSearcher# */{
	/**
	 * This function is called when a result is selected.
	 * @callback Septima.Search.AddressSearcher~selectCallback
	 * @param {Object} result
	 * @param result.title {string}
	 * @param result.description {string}
	 * @param result.data {object} record as returned from service.
	 * @param result.data.addressAccessId {string}
	 * @param result.data.addressSpecificCount int
	 * @param result.data.districtName {string}
	 * @param result.data.districtSubDivisionIdentifier {string}
	 * @param result.data.geometryWkt {string}
	 * @param result.data.mailDeliverySublocationIdentifier {string}
	 * @param result.data.municipalityCode {string}
	 * @param result.data.municipalityName {string}
	 * @param result.data.postCodeIdentifier {string}
	 * @param result.data.presentationString {string}
	 * @param result.data.streetBuildingIdentifier {string}
	 * @param result.data.streetCode {string}
	 * @param result.data.streetName {string}
	 * @param result.data.type {string}
	 * @param result.data.validCoordinates {boolean}
	 * @param result.data.x: int
	 * @param result.data.y: int
	 */
	
	initialize: function (options) {
		if (options === undefined){
			throw "New Septima.Search.AddressSearcher(options): Options missing.";
		}
		
		if (options.area) {
			this.area = options.area;
		}
		options.usesGeoFunctions = true;

		this.Searcher(options);
		//Options
	    this.apikey = options.apiKey; //"3F2504E0-4F89-11D3-9A0C-0305E82C3301";
	    this.title = options.title;
	    this.projection = options.projection; //"25832"
	    //Vars
	    this.serviceUrl ="http://smartadresse.dk/service/locations/3/";
	    this.resource = "detect";
	},
	
	fetchData: function (query, caller){
		var data = {
			apikey: this.apikey,
			limit: query.limit + 1
		};
		if (this.area) {
			data.area = this.area;
		}
		
	    var xhr = jQuery.ajax({
	        url: this.serviceUrl + this.resource + "/json/" + query.queryString,
	        data: data,
	        jsonp: 'callback',
	        dataType: 'jsonp',
	        cache : false,
	        timeout : 10000,
	        crossDomain : true,
	        success : Septima.bind(function (caller, query, data, textStatus,  jqXHR) {
                data.query = query;
	        	this.success (caller, data, textStatus,  jqXHR);
	        },this, caller, query),
	        error : Septima.bind(function (caller, jqXHR, textStatus, errorThrown) {
	        	if (textStatus.toLowerCase() != 'abort' ){
		        	this.error (caller, jqXHR, textStatus, errorThrown);
	        	}
	        },this, caller)     
	    });
	    
	    caller.registerOnCancelHandler ( Septima.bind(function (xhr) {
	    	if (xhr && xhr.readystate != 4){
	    		xhr.abort();
	    	}
	    },this,xhr));
	},
	
	success: function(caller, data, textStatus, jqXHR){
		if (caller.isActive()){
			if (jqXHR.status == 200){
				if (data.status == "OK"){
					caller.fetchSuccess(this.getDataFromSmartResult(data));
				}else{
					caller.fetchError(this, data.message);
				}
			}else{
				caller.fetchError(this, jqXHR.statusText);
			}
		}
	},
	
	getDataFromSmartResult: function (data){
		var queryResult = this.createQueryResult();
		var query = data.query;
		var limit = query.limit;
		var hitType;
		if (data.numHits > 0 ){
			hitType = data.data[0].type;
		}
	    for (var i = 0; i < data.numHits && i < limit; i++){
	        var thisHit = data.data[i];
            var resultGeometry = this.translateWktToGeoJsonObject(thisHit.geometryWkt);
	        if (hitType == 'streetNameType'){
	        	var newquery = thisHit.streetName + " <select>1</select>"  + ', ' + thisHit.postCodeIdentifier + ' ' + thisHit.districtName;
	        	queryResult.addNewQuery(thisHit.presentationString, null, newquery, resultGeometry, thisHit);
	        }else{
	        	queryResult.addResult(thisHit.presentationString, null, resultGeometry, thisHit);
	        }
	    }
        if (hitType == 'streetNameType' && data.numHits > limit && !query.hasTarget){
        	var result = queryResult.addNewQuery("Flere Adresser", "Flere veje matcher <em>" + query.queryString + "</em>", query.queryString, null, null);
        }

	    return queryResult;
	},
	
	error: function(caller, jqXHR, textStatus, errorThrown){
		caller.fetchError(this, errorThrown);
	},

	onSelect: function(result){
    	if (result.data){
    		this._onSelectCallback(result);
    	}
	},

	CLASS_NAME: 'Septima.Search.AddressSearcher'

});