Source: temp/jsdocinputdirs/cercalia.widget.reversegeocoding.js

/**
 * Constructor del widget ReverseGeocoding
 * @class
 * @constructor
 * @param {cercaliax.widget.ReverseGeocodingOptions} options
 */
cercalia.widget.ReverseGeocoding = function(options) {

	/**
	 * Nombre de la classe
	 * @private
	 * @type {string}
	 */
	this.CLASS_NAME_ = 'cercalia.widget.ReverseGeocoding';

	/**
	 * Id del div contenedor.
	 * @private
	 * @type {divId}
	 */
	this.divId_ = options.div ? options.div : null;

	/**
	 * Función que se llama al completar con éxito la llamada a cercalia.
	 * @private
	 * @type {Function}
	 */
	this.completeCallback_ = options.complete ? options.complete : null;

	/**
	 * Función que se llama en caso de error en la llamada a cercalia.
	 * @private
	 * @type {Function}
	 */
	this.errorCallback_ = options.error ? options.error : null;

	/**
	 * @private
	 * @type {cercalia.map}
	 */
	this.map_ = null;

	/**
	 * @private
	 * @type {Array}
	 */
	this.listMarkers_ = null;

	/**
	 * @private
	 * @type {boolean}
	 */
	this.nearestAddress_ = true;

	/**
	 * @private
	 * @type {cercalia.service.Reversegeocoding}
	 */
	this.reversegeocoding_ = null;

	// Inicializamos
	this.initialize_();
};

/**
 * Inicializa el elemento
 * @private
 */
cercalia.widget.ReverseGeocoding.prototype.initialize_ = function() {
	var self = this;
	if (!this.divId_) {
		cercalia.Exception(cercalia.i18n._('WIDGET_ERR_NO_DIV "%"', this.CLASS_NAME_));
	} else {

		// Buscamos div
		var element = cercalia.jQuery('#' + this.divId_).addClass('cercalia-widget cercalia-widget-reversegeocoding');
		this.reversegeocoding_ = new cercalia.service.ReverseGeocoding();
		this.listMarkers_ = [];

		var divInput = cercalia.jQuery('<div />').addClass('deleteicon').appendTo(element);

		// Creamos input, br y select de pais
		var inputX = cercalia.jQuery('<input />')
			.attr('type', 'text')
			.attr('placeholder', cercalia.i18n._('REVERSEGEOCODING_X_PLACEHOLDER'))
			.attr('id', this.divId_ + '_coordX')
			.addClass('cercalia-widget-reversegeocoding-coordX')
			.appendTo(divInput);

		//icona per borrar l'input
		jQuery('<span/>').addClass('icon').click(function() {
			jQuery(this).prev('input').val('').focus();
			self.autocomplete_.close();
			input.parent().find('span').hide();
		}).appendTo(divInput);

		inputX.parent().find('span').hide();
		inputX.keyup(function() {
			if (jQuery(this).val().length > 0) inputX.parent().find('span').show();
			else inputX.parent().find('span').hide();
		});


		cercalia.jQuery('<br />').appendTo(element);

		var divInput = cercalia.jQuery('<div />').addClass('deleteicon').appendTo(element);

		var inputY = cercalia.jQuery('<input />')
			.attr('type', 'text')
			.attr('placeholder', cercalia.i18n._('REVERSEGEOCODING_Y_PLACEHOLDER'))
			.attr('id', this.divId_ + '_coordY')
			.addClass('cercalia-widget-reversegeocoding-coordY')
			.appendTo(divInput);

		//icona per borrar l'input
		jQuery('<span/>').addClass('icon').click(function() {
			jQuery(this).prev('input').val('').focus();
			self.autocomplete_.close();
			input.parent().find('span').hide();
		}).appendTo(divInput);

		inputY.keyup(function() {
			if (jQuery(this).val().length > 0) inputY.parent().find('span').show();
			else inputY.parent().find('span').hide();
		});
		inputY.parent().find('span').hide();


		cercalia.jQuery('<br />').appendTo(element);

		//Select SRS
		var selectSRS = cercalia.jQuery('<select />')
			.attr('id', this.divId_ + '_srs')
			.addClass('cercalia-widget-reversegeocoding-srs')
			.appendTo(element);

		cercalia.jQuery('<br />').appendTo(element);

		//Nearest address checkbox
		var nearestAddressInputId = this.divId_ + '_nearestAddress';
		cercalia.jQuery('<div />')
			.addClass('cercalia-widget-reversegeocoding-nearestaddress')
			.append(
				cercalia.jQuery('<label />')
					.attr('for', nearestAddressInputId)
					.text('Nearest address')
			)
			.append(
				cercalia.jQuery('<input />')
					.attr('id', nearestAddressInputId)
					.prop('checked', true)
					.attr('type', 'checkbox')
					.click(function() {
						self.nearestAddress_ = this.checked;
					})
			)
		.appendTo(element);


		//Submit button
		var buttonBuscar = cercalia.jQuery('<button />')
			.attr('id', this.divId_ + '_find')
			.addClass('cercalia-widget-reversegeocoding-find')
			.html(cercalia.i18n._('REVERSEGEOCODING_FIND'))
			.appendTo(element)
			.button();

		// Llenamos el select
		cercalia.jQuery('<option />').html('EPSG:4326').attr('value', 'EPSG:4326').appendTo(selectSRS);
		cercalia.jQuery('<option />').html('EPSG:3857').attr('value', 'EPSG:3857').appendTo(selectSRS);

		buttonBuscar.click(function() {
			var x = inputX.val();
			var y = inputY.val();
			var srs = selectSRS.val();
			var position = new cercalia.LonLat(x, y, srs);

			self.reversegeocoding_.getDirection(position, function(result) {
				if (typeof(result.cercalia.error) !== 'undefined' && result.cercalia.error !== null) {

					if (self.errorCallback_ != null) {
						self.errorCallback_(result);
					} else {
						self.callbackErrorfunction_(result);
					}

				} else {

					if (self.completeCallback_ != null) {
						self.completeCallback_(result);
					} else if (self.map_ != null) {
						self.callbackfunction_(result, position);
					} else {
						cercalia.Exception(cercalia.i18n._('WIDGET_ERR_NO_MAP_NO_FUNCTION '%'', this.CLASS_NAME_));
					}
				}
			});
		});
	}
};

/**
 * Limpia el input y select
 */
cercalia.widget.ReverseGeocoding.prototype.clean = function() {
	cercalia.jQuery('#' + this.divId_ + '_coordX').val('');
	cercalia.jQuery('#' + this.divId_ + '_coordY').val('');
	this.cleanMarkers();
};

/**
 * Modifica el mapa con el que interactua el widget.
 * @param {cercalia.Map} map Mapa con el que interactuará el Widget.
 */
cercalia.widget.ReverseGeocoding.prototype.setMap = function(map) {
	this.map_ = map;
};

/**
 * Limpia las busquedas activas de geocoding.
 */
cercalia.widget.ReverseGeocoding.prototype.cleanMarkers = function() {
	for (var index = 0; index < this.listMarkers_.length; index++) {
		var marker = this.listMarkers_[index];
		marker.destroy();
	}

	this.listMarkers_ = [];
};

/**
 * Funcion de callback llamada por defecto.
 * @private
 * @param {Object} result Resultado de cercalia
 * @param {cercalia.LonLat} position Position.
 */
cercalia.widget.ReverseGeocoding.prototype.callbackfunction_ = function(result, position) {
	var popupContent = function(ge) {
		var content = cercalia.jQuery('<div />');
		var table = cercalia.jQuery('<table />').appendTo(content);

		if (ge.city) {
			var tr = cercalia.jQuery('<tr />').appendTo(table);
			cercalia.jQuery('<td />').css('font-weight', 'bold').html(cercalia.i18n._('City')).appendTo(tr);
			cercalia.jQuery('<td />').html(ge.city.value).appendTo(tr);
		}

		if (ge.postalcode) {
			var tr = cercalia.jQuery('<tr />').appendTo(table);
			cercalia.jQuery('<td />').css('font-weight', 'bold').html(cercalia.i18n._('Postal code')).appendTo(tr);
			cercalia.jQuery('<td />').html(ge.postalcode.id).appendTo(tr);
		}

		if (ge.municipality) {
			var tr = cercalia.jQuery('<tr />').appendTo(table);
			cercalia.jQuery('<td />').css('font-weight', 'bold').html(cercalia.i18n._('Municipality')).appendTo(tr);
			cercalia.jQuery('<td />').html(ge.municipality.value).appendTo(tr);
		}

		if (ge.subregion) {
			var tr = cercalia.jQuery('<tr />').appendTo(table);
			cercalia.jQuery('<td />').css('font-weight', 'bold').html(cercalia.i18n._('Subregion')).appendTo(tr);
			cercalia.jQuery('<td />').html(ge.subregion.value).appendTo(tr);
		}

		if (ge.region) {
			var tr = cercalia.jQuery('<tr />').appendTo(table);
			cercalia.jQuery('<td />').css('font-weight', 'bold').html(cercalia.i18n._('Region')).appendTo(tr);
			cercalia.jQuery('<td />').html(ge.region.value).appendTo(tr);
		}

		if (ge.country) {
			var tr = cercalia.jQuery('<tr />').appendTo(table);
			cercalia.jQuery('<td />').css('font-weight', 'bold').html(cercalia.i18n._('Country')).appendTo(tr);
			cercalia.jQuery('<td />').html(ge.country.value).appendTo(tr);
		}

		if (ge.coord) {
			var tr = cercalia.jQuery('<tr />').appendTo(table);
			cercalia.jQuery('<td />').css('font-weight', 'bold').html(cercalia.i18n._('Coords')).appendTo(tr);
			cercalia.jQuery('<td />').html(ge.coord.x + ', ' + ge.coord.y).appendTo(tr);
		}

		return content.html();
	};

	var ge = result.cercalia.proximity.gelist.ge;
	var popup = new cercalia.Popup({
		visible: true,
		title: 'Place',
		content: popupContent(ge)
	});

	var markerPosition = this.nearestAddress_ ? new cercalia.LonLat(ge.coord.x, ge.coord.y) : position;
	var marker = new cercalia.Marker({
		position: markerPosition,
		popup: popup
	});

	this.map_.addMarker(marker);
	this.listMarkers_.push(marker);

	this.map_.panTo(marker.getPosition(), cercalia.MapAnimation.PAN);
	this.map_.setZoom(12, true);
};

/**
 * Funcion de callback de error llamada por defecto.
 * @private
 * @param {Object} result
 */
cercalia.widget.ReverseGeocoding.prototype.callbackErrorfunction_ = function(result) {
	alert('ReverseGeocoding - Cercalia error (' + result.cercalia.error.id + '): ' + result.cercalia.error.value);
	console.error(result.cercalia.error);
};

/**
 * Devuelve el tipo del objeto.
 * @return {string}
 */
cercalia.widget.ReverseGeocoding.prototype.getClass = function() {
	return this.CLASS_NAME_;
};