将Google Api多边形保存为openlayers字符串

时间:2015-08-14 15:26:39

标签: google-maps google-maps-api-3 openlayers

我想保存用户将其绘制到字符串中的多边形,如:

"SRID=4326;GEOMETRYCOLLECTION(POLYGON((......)))"

反正有吗? 我不想使用google API中的encodeString

感谢

1 个答案:

答案 0 :(得分:1)

我找到了这个解决方案,并希望这也可以帮助那些与我有同样问题的人。

Convert Google Maps Polygon (API V3) to Well Known Text (WKT) Geometry Expression

function GMapPolygonToWKT(poly) {
    // Start the Polygon Well Known Text (WKT) expression
    wkt = "SRID=4326;GEOMETRYCOLLECTION(POLYGON(";

    var paths = poly.getPaths();

    for(var i=0; i<paths.getLength(); i++) {
        var path = paths.getAt(i);

        // Open a ring grouping in the Polygon Well Known Text
        wkt += "(";
        for(var j=0; j<path.getLength(); j++) {

            // setCenteradd each vertice and anticipate another vertice (trailing comma)
            wkt += path.getAt(j).lng().toString()
                + " "
                + path.getAt(j).lat().toString()
                +",";
        }
        // Also close the ring grouping and anticipate another ring (trailing comma)
        wkt += path.getAt(0).lng().toString()
            + " "
            + path.getAt(0).lat().toString()
            + "),";
    }
    // resolve the last trailing "," and close the Polygon
    wkt = wkt.substring(0, wkt.length - 1) + "))";
    return wkt;
}

这是GeoDjango postgis数据库的有效字符串。