I have published a wfs layer in geoserver, and displayed that on the google map using openlayers. By using ol3 i am able to select the polygon, but iam not able to get the coordinates of the polygon. For selecting the polygon my code is

    var interaction;
    var select = new ol.interaction.Select({
        style: new ol.style.Style({
            stroke: new ol.style.Stroke({
                color: '#FF2828'

              interaction = new ol.interaction.Select({
        style: new ol.style.Style({
            stroke: new ol.style.Stroke({color: '#f50057', width: 2})


How to get the cocordinates of the selected polygon in geojson format?

  • 1
    You can't select geometries over WMS (unless you make a modification to make a GetFeatureInfo query), are you using WFS? – cag Nov 4 '16 at 6:45
  • sorry i am using wfs only, by mistake i have mentioned wms – user46218 Nov 4 '16 at 7:16

First, to get the selected features, use the getFeatures method from the select interaction object. Then, to get the geojson representation of this features, OL provides the ol.format.GeoJSON class, which you can use to convert the features (or only his geometries) to a json string.

Here is an example:

var toGeoJSON = new ol.format.GeoJSON();
var selectedFeatures = interaction.getFeatures();

    // To get the geojson representation of the geometry:
    var geomJSON = toGeoJSON.writeGeometry(feature.getGeometry());

    // To get the geojson representation of the feature:
    var featureJson = toGeoJSON.writeFeature(feature);


Working example on fiddle: https://jsfiddle.net/1Lpqxc53/

  • How to declare a feature in your code?Because when i run your code i am getting error as "feature.getGeometry() is not a function" – user46218 Nov 7 '16 at 4:09
  • I've added a working example, now you can see how it works. – cag Nov 7 '16 at 6:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.