Last active
December 18, 2015 10:19
-
-
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))
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.