How to draw a polygon in OpenLayers 3?

This code is working correctly. where vertices are the array of coordinates

var feature = new ol.Feature({
            geometry: new ol.geom.Polygon([vertices])
        });

feature.getGeometry().transform('EPSG:4326', 'EPSG:3857');
var vectorSource= new ol.source.Vector({
        features: [feature ]
    });

 var vectorLayer = new ol.layer.Vector({
        source: vectorSource
    });
 var map = new ol.Map({
        target: $('#map')[0],
        layers: [
            new ol.layer.Tile({
                source: new ol.source.TileJSON({
                    url: 'http://api.tiles.mapbox.com/v3/mapbox.geography-class.jsonp'
                })
            }),
            vectorLayer 
        ],
        view: new ol.View({
            center: ol.proj.transform([104.07143559628913, 30.669867138240782], 'EPSG:4326', 'EPSG:3857'),
            zoom: 12
        })
    });

Demo

Tags:

Openlayers