-1

I want to save my polygon which user draws it into the strings like:

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

Is there anyway to do it ? I don't want to use encodeString from google API

thanks

GeoCom
  • 1,290
  • 2
  • 12
  • 33
  • The answer to [Convert Well-known text (WKT) from MySQL to Google Maps polygons](http://stackoverflow.com/questions/16482303/convert-well-known-text-wkt-from-mysql-to-google-maps-polygons-with-php/18112446#18112446) might be relevant. – geocodezip Aug 14 '15 at 15:49

1 Answers1

1

I found this solution and hope this also can help anybody out there with the same problem as mine.

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;
}

This is a valid string for GeoDjango postgis database.

GeoCom
  • 1,290
  • 2
  • 12
  • 33
  • Your function accepts an argument `poly`, which is then completely ignored. But it does refer to a variable `geoField`; where did that come from? The original version of this code used that argument: `var paths = poly.getPaths();`, why not just pass `geoField` into the function when you call it? – duncan Aug 27 '15 at 12:36
  • yes, you are right I changed it to poly, geoField is my global variable which not related to this answer. – GeoCom Aug 27 '15 at 12:39