# Source: bemap-ol/bemap-map-ol-polygon.js

    /**
     * Add a bemap.Polygon to the layer
     * @public
     * @param {bemap.Polygon} bemap.Polygon.
     * @param {object} options
     * @return {bemap.OlMap}
     */
    bemap.OlMap.prototype.addPolygon = function(polygon, options) {
        if (polygon && bemap.inheritsof(polygon, bemap.Polygon)) {
            polygon.native = new ol.Feature({
                geometry: new ol.geom.Polygon([polygon.getLonLatArrays()]),
            });

            if (polygon.map === null) {
                polygon.map = this;
            }

            if (polygon.id) {
                polygon.native.setId(polygon.id);
            }

            bemap.OlMap.prototype._addOwnToProperties(polygon);

            polygon.native.getGeometry().transform(bemap.Map.PROJ.EPSG_WGS84, this.native.getView().getProjection());

            if (polygon.style) {
                if (polygon.style.native === null) {
                    this.buildPolygonStyle(polygon.style);
                }
                polygon.native.setStyle(polygon.style.native);
            }

            var opts = options || {};
            var l = null;

            if (opts.layer && bemap.inheritsof(opts.layer, bemap.VectorLayer)) {
                l = opts.layer;
            } else {
                l = this.getLayerByName(bemap.Map.DEFAULT_LAYER.POLYGON);
                if (l === null) {
                    l = new bemap.VectorLayer({
                        name: bemap.Map.DEFAULT_LAYER.POLYGON
                    });
                    this.addLayer(l);
                }
            }

            if (polygon.layer === null) {
                polygon.layer = l;
            }

            l.native.getSource().addFeature(polygon.native);
        }
        return this;
    };

    /**
     * Remove a polygon from his layer.
     * @public
     * @param {bemap.Polygon} polygon the polygon object to remove.
     */
    bemap.OlMap.prototype.removePolygon = function(polygon) {
        if (polygon && polygon.layer && polygon.layer.native && bemap.inheritsof(polygon, bemap.Polygon)) {
            polygon.layer.native.getSource().removeFeature(polygon.native);
            polygon.layer = null;
            polygon.map = null;
        }
        return this;
    };

    /**
     * Set the listner when an specified eventType occur on bemap.Polygon.
     * @public
     * @param {bemap.Polygon} polygon
     * @param {bemap.Map.EventType} eventType Event type.
     * @param {function} callback Function will be called when the specified eventType is occur.
     * @param {object} options options.
     * @return {bemap.Listener} this.
     */
    bemap.OlMap.prototype.onPolygon = function(polygon, eventType, callback, options) {
        return this._onFeature(polygon, eventType, callback, options, {
            singleFeature: true
        });
    };

    /**
     * Set the listner when an specified eventType occur on all bemap.Polygon.
     * @public
     * @param {bemap.Map.EventType} eventType Event type.
     * @param {function} callback Function will be called when the specified eventType is occur.
     * @param {object} options options.
     * @return {bemap.Listener} this.
     */
    bemap.OlMap.prototype.onPolygons = function(eventType, callback, options) {
        return this._onFeature(null, eventType, callback, options, {
            polygons: true
        });
    };

    /**
     * Define the draggable capability for bemap.Polygon.
     * @protected
     * @param {bemap.Polygon} polygon bemap object.
     * @param {function} callback Function will be called when the specified eventType is occur.
     * @param {object} options Options.
     * @param {bemap.Layer} options.layerFilter set the bemap layer used as filter.
     * @return {bemap.Listener} bemap.listener.
     */
    bemap.OlMap.prototype.draggablePolygon = function(polygon, callback, options) {
        return this._draggableFeature(polygon, callback, options, {
            singleFeature: true
        });
    };

    /**
     * Define the draggable capability for all bemap.Polygon.
     * @protected
     * @param {function} callback Function will be called when the specified eventType is occur.
     * @param {object} options Options.
     * @param {bemap.Layer} options.layerFilter set the bemap layer used as filter.
     * @return {bemap.Listener} bemap.listener.
     */
    bemap.OlMap.prototype.draggablePolygons = function(callback, options) {
        return this._draggableFeature(null, callback, options, {
            polygons: true
        });
    };

    /**
     * Update the coordinates from the native map browser.
     * @public
     */
    bemap.OlMap.prototype.updatePolygonCoordinates = function(polygon) {
      var pol = polygon.native.getGeometry().getCoordinates(true)[0];

      for (var i = 0; i < pol.length; i++) {
        var polc = ol.proj.transform(pol[i], this.native.getView().getProjection(), bemap.Map.PROJ.EPSG_WGS84);
        var coord = polygon.coords[i];

        if(coord && coord !== null && bemap.inheritsof(coord, bemap.Coordinate)) {
          coord.setLon(polc[0]);
          coord.setLat(polc[1]);
        } else {
          polygon.coords[i] = new bemap.Coordinate(polc[0], polc[1]);
       }
      }

      if(polygon.coords.length > pol.length) {
        for (var j = pol.length; j < polygon.coords.length; j++) {
          polygon.coords.pop();
        }
      }
    };