将 Google Api 多边形保存为 openlayers 字符串
Save Google Api polygon as openlayers string
我想将用户绘制的多边形保存到字符串中,例如:
"SRID=4326;GEOMETRYCOLLECTION(POLYGON((......)))"
有办法吗?
我不想使用 google API
中的 encodeString
谢谢
我找到了这个解决方案,希望这也能帮助遇到与我相同问题的任何人。
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 数据库的有效字符串。
我想将用户绘制的多边形保存到字符串中,例如:
"SRID=4326;GEOMETRYCOLLECTION(POLYGON((......)))"
有办法吗? 我不想使用 google API
中的 encodeString谢谢
我找到了这个解决方案,希望这也能帮助遇到与我相同问题的任何人。
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 数据库的有效字符串。