Archive for the ‘JavaScript’ Category
Convert Google Maps Polygon (API V3) to Well Known Text (WKT) Geometry Expression
There’s dozens of reasons why you might want the Well Known Text (WKT) geometry expression for a Google Maps Polygon object.
Assuming you’re using the Google Maps API V3, and you’ve got a variable referencing your Polygon, I’ll suggest two approaches you can take to iterate over the paths and vertices in your Google Maps polygon and return the geometry expression as a Well Known Text string.
Add a Simple Utility Method to Your Project
Easy enough. Just add the following method to your project. Look below the method for an example of how you’d call it.
function GMapPolygonToWKT(poly)
{
// Start the Polygon Well Known Text (WKT) expression
var wkt = "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++)
{
// add each vertice and anticipate another vertice (trailing comma)
wkt += path.getAt(j).lng().toString() +" "+ path.getAt(j).lat().toString() +",";
}
// Google's approach assumes the closing point is the same as the opening
// point for any given ring, so we have to refer back to the initial point
// and append it to the end of our polygon wkt, properly closing it.
//
// 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;
}
Here’s how you’d access the Well Known Text expression using the utility method:
// Assuming you've already instantiated "myPolygon" somewhere.
var wkt = GMapPolygonToWKT(myPolygon);
Extend Google’s Polygon Object Prototype with a ToWKT() Method
There’s nothing wrong with the first approach, but you might find it handy to extend Google’s Polygon object prototype, itself, to include a ToWKT() member function, which makes it even easier to get its Well Known Text. To do that, add the following JavaScript somewhere near the top of your code (caveat—this will need to be called after the Google Maps library has been loaded):
if (typeof google.maps.Polygon.prototype.ToWKT !== 'function') { google.maps.Polygon.prototype.ToWKT = function() { var poly = this; // Start the Polygon Well Known Text (WKT) expression var wkt = "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++) { // add each vertice, automatically anticipating another vertice (trailing comma) wkt += path.getAt(j).lng().toString() + " " + path.getAt(j).lat().toString() + ","; } // Google's approach assumes the closing point is the same as the opening // point for any given ring, so we have to refer back to the initial point // and append it to the end of our polygon wkt, properly closing it. // // Additionally, 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; }; }
If you prefer the second approach, you can get the Well Known Text expression like this:
// Assuming you've already instantiated "myPolygon" somewhere.
var wkt = myPolygon.ToWKT();