/**
* POIs service constructor.
*
* Use this class for getting POIs specifying the IDs, or getting a POI list from an area (WKT or map bounds (cercalia.Bounds))
* and the POI category
*
* @class
* @constructor
* @param {cercaliax.service.PoisOptions} options Opciones Geocoding
*/
cercalia.service.Pois = function(options) {
options = options ? options : {};
/**
* Class name
* @private
* @type {string}
*/
this.CLASS_NAME_ = "cercalia.service.Pois";
/**
* @private
* @type {string}
*/
this.cercaliaKey_ = cercaliaGlobals.key; //Esta variable siempre se pasara al descargar la API
/**
* @private
* @type {string}
*/
this.server_ = servers.servlet;
/**
* @private
* @type {Array.<string>}
*/
this.pois_ = options.pois ? options.pois : [];
/**
* @private
* @type {Array.<string>}
*/
this.categories_ = options.categories ? options.categories : [];
/**
* @private
* @type {string}
*/
this.wkt_ = options.wkt ? options.wkt : null;
/**
* @private
* @type {string}
*/
this.buffer_ = options.buffer ? options.buffer : null;
/**
* @private
* @type {cercalia.Bounds}
*/
this.bounds_ = options.bounds ? options.bounds : null;
/**
* @private
* @type {boolean}
*/
this.searchByScale_ = options.searchByScale ? options.searchByScale : false;
/**
* @private
* @type {number}
*/
this.width_ = options.width ? options.width : null;
/**
* @private
* @type {number}
*/
this.height_ = options.height ? options.height : null;
/**
* @private
* @type {string}
*/
this.gridsize_ = options.gridsize ? options.gridsize : null;
/**
* @private
* @type {Object}
*/
this.lastXhr_ = null;
/**
* @private
* @type {string}
*/
this.response_ = null;
};
/**
* Returns the POIs list
* @return {Array.<string>} Returns a POI IDs array
*/
cercalia.service.Pois.prototype.getQueryPois = function() {
return this.pois_;
};
/**
* Set the POIs list to search.
* @param {Array.<string>} pois POIs ID list, in an array
*/
cercalia.service.Pois.prototype.setQueryCategories = function(categories) {
this.categories_ = categories;
};
/**
* Returns the POIs categories to search
* @return {Array.<string>} categories list string
*/
cercalia.service.Pois.prototype.getQueryCategories = function() {
return this.categories_;
};
/**
* Set the POIs categories to search.
* @param {Array.<string>} categories categories list string
*/
cercalia.service.Pois.prototype.setQueryPois = function(categories) {
this.categories_ = categories;
};
/**
* Returns the map area corresponding to the spatial query. The format is WKT
* @return {Array.<string>} Geometria WKT (string)
*/
cercalia.service.Pois.prototype.getWkt = function() {
return this.wkt_;
};
/**
* Set the polygon for the spatial query. String WKT format
* If spatial query use a WKT, the map bounds will be ignored and must be reset next time
* @param {Array.<string>} wkt WKT geometry
*/
cercalia.service.Pois.prototype.setWkt = function(wkt) {
this.bounds_ = null;
this.wkt_ = wkt;
};
/**
* Returns the WKT of the spatial query
* @return {cercalia.LonLat} Coordinate
*/
cercalia.service.Pois.prototype.getBounds = function() {
return this.bounds_;
};
/**
* Set the polygon for the spatial query. String WKT format.
* If spatial query use map bounds, the WKT will be ignored and must be reset next time
* @param {cercalia.LonLat} wkt WKT string
*/
cercalia.service.Pois.prototype.setBounds = function(bounds) {
this.wkt_ = null;
this.bounds_ = bounds;
};
cercalia.service.Pois.prototype.clearResponse = function(){
this.response_ = null;
};
/**
* Returns the response with the 'geocode' function request
* @return {Object} response
*/
cercalia.service.Pois.prototype.getResponse = function() {
return this.response_;
};
/**
* @param {boolean} searchByScale
*/
cercalia.service.Pois.prototype.setSearchbyScale = function(searchByScale) {
this.searchByScale_ = searchByScale;
};
/**
*
* @return {boolean} boolean that shows if POIs by scale search is used
*/
cercalia.service.Pois.prototype.isSearchbyScale = function() {
return this.searchByScale_;
};
/**
* @return {number} width
*/
cercalia.service.Pois.prototype.getWidth = function() {
return this.width_;
};
/**
* @param {number} width
*/
cercalia.service.Pois.prototype.setWidth = function(width) {
this.width_ = width;
};
/**
* @return {number} height
*/
cercalia.service.Pois.prototype.getHeigth = function() {
return this.height_;
};
/**
* @param {number} height
*/
cercalia.service.Pois.prototype.setHeight = function(height) {
this.height_ = height;
};
/**
* @return {number} gridsize
*/
cercalia.service.Pois.prototype.getGridsize = function() {
return this.gridsize_;
};
/**
* @param {number} gridsize
*/
cercalia.service.Pois.prototype.setGridsize = function(gridsize) {
this.gridsize_ = gridsize;
};
/**
* Execute the server request and the callback function to be processed with the response
* @param {Function} callbackFn Funcion callback
*/
cercalia.service.Pois.prototype.doPoisRequest = function(callbackFn) {
//Si buscamos por categoria
if( this.categories_.length > 0 ) {
//Queremos obtener por extension (extent o bounds)
if( this.bounds_ ) {
var pointNW = this.bounds_.getNorthWest();
var pointSE = this.bounds_.getSouthEast();
var extTopLeft = pointNW.getLat() + "," + pointNW.getLon();
var extBottomRight = pointSE.getLat() + "," + pointSE.getLon();
//Obtener todos los POIs acorde a una extension. Sin limitarlos dependiendo la escala
//Puede devolver error del servidor (o tardar mucho en caso que se devuelvan demasiados).
if(!this.searchByScale_) {
var params = {
cmd : 'pois',
key : this.cercaliaKey_,
searchIn : 'bounds',
getpoicats : this.categories_.join(","),
extent : extTopLeft + "|" + extBottomRight
};
this.ajaxQuery_(params, callbackFn);
//Devuelve una cantidad de POIs acorde con la extension.
} else {
var params = {
cmd : 'pois',
key : this.cercaliaKey_,
searchIn : 'bounds',
gpoicats : this.categories_.join(","),
extent : extTopLeft + "|" + extBottomRight,
width : this.width_,
height : this.height_
};
if(this.gridsize_){
params.gridsize = this.gridsize_;
}
this.ajaxQuery_(params, callbackFn);
}
//Devuelve todos los POIs de las categorias especificadas que se encuentran dentro un poligono (wkt)
} else if ( this.wkt_ ) {
var params = {
key : this.cercaliaKey_,
cmd : 'pois',
searchIn : 'wkt',
getpoicats : this.categories_.join(","),
wkt : this.wkt_
};
if(this.buffer_){
params.buffer = this.buffer_;
}
this.ajaxQuery_(params, callbackFn);
} else {
alert('cercalia.service.pois: Caso no contemplado!');
}
//Devuelve los POIs especificados dentro del Array 'this.pois_'
} else if ( this.pois_.length > 0 ) {
var params = {
cmd : 'pois',
key : this.cercaliaKey_,
poilist : this.pois_.join(",")
};
this.ajaxQuery_(params,callbackFn);
}
};
/**
* @private
* @param {Object} params
* @param {Function} callbackFn
*/
cercalia.service.Pois.prototype.ajaxQuery_ = function(params, callbackFn){
var self = this;
this.lastXhr_ = cercalia.jQuery.ajax({
url: this.server_,
type: this.wkt_ ? 'POST' : 'GET',
method: this.wkt_ ? 'POST':'GET', //The HTTP method to use for the request (e.g. "POST", "GET", "PUT"). (version added: 1.9.0)
data: params,
dataType: this.wkt_ ? "json" : cercalia.Util.isIEUnderTen() ? "jsonp" : "json",
//contentType: 'application/json; charset=utf-8',
async:true,
crossDomain: true,
timeout: 15000,
success: function(data){
self.response_ = data;
// console.group("doPoisRequest - response");
// console.log(params);
// console.log(data);
// console.groupEnd();
if(callbackFn){
callbackFn(data);
} else {
console.error("No callbackfunction defined");
}
}
//error: cercalia.AJAXException
});
};
/**
* Abort the last AJAX request if it hasn't been resolved.
*/
cercalia.service.Pois.prototype.abortLast = function(){
if(this.lastXhr_ && this.lastXhr_.readyState < 4) {
this.lastXhr_.abort();
}
};
/**
* Returns the object type.
* @return {string}
*/
cercalia.service.Pois.prototype.getClass = function(){
return this.CLASS_NAME_;
};