1

我有一个可以绘制多边形的 openlayers 3 地图。

我会返回代表绘制多边形的 WKT 字符串。

我该怎么做?

JSFiddle 代码http://jsfiddle.net/michelejs/3zawt33b/7/

这是我的地图:

map = new ol.Map({
            target: 'map',
            layers: [raster,vector],
            view: new ol.View({
              center: ol.proj.fromLonLat([11.249367, 43.774298]),
              zoom: 15
            })
        });

这里是帮助我绘制多边形的 intaractions:

function addInteraction() {

        var ct = 0;
        draw = new ol.interaction.Draw({
            source: source,
            type: 'Polygon',
            geometryFunction: function (c, g) {
                if (goog.isDef(g)) {
                    g.setCoordinates(c);
                } else {
                    g = new ol.geom.Polygon(c);
                }
                if (c[0].length > ct) {
                    console.log('click coord : ' + c[0][c[0].length - 1]);
                    var coord = c[0][c[0].length - 1];
                    $('div#coordinate').html( $('div#coordinate').html() + "<p>" + ( Number(coord[0]).toFixed(2) ) + " - " + ( Number(coord[1]).toFixed(2) ) + "</p>" );
                coordinates.push(coord);
                    ct = c[0].length;
                } else {
                    console.log('move coord : ' + c[0][c[0].length - 1]);

                }

                return g;
            }
        });
        draw.on('drawend',  function(e) {
            isin = e;
            checkIfIn();
            lastFeature = e.feature;

          //write WKT Polygon Code in div#getAsWK
        })

        draw.on('drawstart', function (e) {
            source.clear();
        });


        map.addInteraction(draw);

    }
   map.addInteraction(draw);
4

1 回答 1

4

ol3 包含用于此目的的 ol.format.WKT 类。像这样使用 writeGeometry() 方法:

var format = new ol.format.WKT(),
    wkt = format.writeGeometry(yourFeature.getGeometry());

请参阅 API 文档: http: //openlayers.org/en/v3.0.0/apidoc/ol.format.WKT.html#writeGeometry

小提琴:http: //jsfiddle.net/igor23/3zawt33b/9/

于 2016-02-08T04:31:31.587 回答