Skip to content

Instantly share code, notes, and snippets.

@derickito
Last active December 18, 2015 10:19
Show Gist options
  • Save derickito/5767911 to your computer and use it in GitHub Desktop.
Save derickito/5767911 to your computer and use it in GitHub Desktop.
This function converts a Google Maps V3 (Javascript API) of MVCArray of lats and longs (MVCArray<LatLng>) to Well Known Text (WKT). The result will be something like POLYGON((50 0, 0 20, 10 10, 50 0))
function MVCToWKT(arr){
var result = 'POLYGON(';
for(ring = 0; ring < arr.length; ring++) {
var strRing = '(';
for(i = 0; i < arr.getAt(ring).length; i++) {
strRing += (i > 0 ? ", " : "") + arr.getAt(ring).getAt(i).lng() + ' ' + arr.getAt(ring).getAt(i).lat();
}
//Add the first point to the end to close polygon
strRing += ', '+arr.getAt(ring).getAt(0).lng() + ' ' + arr.getAt(ring).getAt(0).lat();
strRing += ')';
result += (ring > 0 ? ", " : "")+strRing;
}
result += ')';
return result;
}
@derickito
Copy link
Author

I made this function because I couldn't find an easy way to convert a Google polygon to the WKT format in order to use it to save it to MySQL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment