define([
    'backbone',
    'fieldtext/js/field-text-parser',
    'find/app/configuration'
], function(Backbone, parser, configuration) {

    return Backbone.Model.extend({
        /**
         * @typedef {Object} GeographyModelAttributes
         * @property {?Array} shapes
         */
        /**
         * @type GeographyModelAttributes
         */
        defaults: {
            shapes: []
        },

        appendFieldText: function(existingFieldText){
            const toAppend = this.toFieldText();

            if (toAppend) {
                return existingFieldText ? toAppend.AND(toAppend) : toAppend;
            }

            return existingFieldText;
        },

        /**
         * Convert this model to fieldtext queries
         */
        toFieldText: function() {
            const shapes = this.get('shapes');
            if (!shapes || !shapes.length) {
                return null;
            }

            const fieldNodes = [];

            const fieldsInfo = configuration().fieldsInfo;

            // TODO: read map config for the latitude field
            // TODO: what if they have multiple lat and lon fields?
            // TODO: what if a lat/lon field has multiple areas?
            // const LAT = fieldsInfo.latitude && fieldsInfo.latitude.names && fieldsInfo.latitude.names[0]
            // const LON = fieldsInfo.longitude && fieldsInfo.longitude.names && fieldsInfo.longitude.names[0]
            const LAT = 'lat';
            const LON = 'lon';

            if (!LAT || !LON) {
                return null;
            }

            _.each(shapes, function (shape) {
                if (shape.type === 'circle') {
                    fieldNodes.push(new parser.ExpressionNode('DISTSPHERICAL', [ LAT, LON ], [
                        shape.center[0],
                        shape.center[1],
                        Math.round(shape.radius / 1000) // IDOL uses kilometers, while we use meters
                    ]));
                }
                else if (shape.type === 'polygon') {
                    var points = _.flatten(shape.points);
                    fieldNodes.push(new parser.ExpressionNode('POLYGON', [ LAT, LON ], points));
                }
            });

            if (fieldNodes.length) {
                return _.reduce(fieldNodes, parser.OR);
            } else {
                return null;
            }
        }
    });

});
@@ -978,26 +978,22 @@
   const 
-fieldsInfo
+config
  = confi
@@ -1006,81 +1006,304 @@
 on()
-.fieldsInfo;%0A%0A            // TODO: read map config for the latitude field
+;%0A            const fieldsInfo = config.fieldsInfo;%0A%0A            let LAT, LON;%0A%0A            function getFieldName(fieldName) %7B%0A                const fieldMeta = fieldsInfo%5BfieldName%5D;%0A                if (fieldMeta) %7B%0A                    return fieldMeta.names%5B0%5D;%0A                %7D%0A            %7D%0A
 %0A   
@@ -1429,12 +1429,12 @@
 ple 
-area
+name
 s?%0A 
@@ -1448,103 +1448,120 @@
     
-// const LAT = fieldsInfo.latitude && fieldsInfo.latitude.names && fieldsInfo.latitude.name
+if (config.map && config.map.locationFields) %7B%0A                const firstField = config.map.locationField
 s%5B0%5D
+;%0A
 %0A   
@@ -1573,87 +1573,140 @@
     
-// const LON = fieldsInfo.longitude && fieldsInfo.longitude.names && fieldsInfo
+    if (firstField) %7B%0A                    const latField = firstField.latitudeField;%0A                    const lonField = firstField
 .lon
@@ -1711,25 +1711,23 @@
 ongitude
-.names%5B0%5D
+Field;%0A
 %0A       
@@ -1735,58 +1735,188 @@
     
-const LAT = 'lat';%0A            const LON = 'lon';%0A
+        LAT = getFieldName(latField);%0A                    LON = getFieldName(lonField);%0A                %7D%0A            %7D%0A%0A            // TODO: read map config for the latitude field
 %0A   

Read the field config from the configuration