fredag 20 oktober 2017

What does geometry.toString() mean?


In web development with JavaScript or similar, what is the meaning of geometry.toString()? I was developing a small application and started to think may be it is time to do something more interesting than [object] or Uncaught Not implemented. What if toString() would return the actual string representation of the object? That would make toString() useful.

Since JSON is the natural representation of the object would it  make sense to simple return a stringified GeoJson? I think so. For polygon the implementation would look something as:

Microsoft.Maps.Polygon.prototype.toString = function () {
    var _json = Microsoft.Maps.GeoJson.write(this);
    return JSON.stringify(_json);

Does it make sense? At least it does for me since I can just write geom.toString() when I need to communicate with the server.

Of course there are alternatives to GeoJSON, WKT, well known text for example. I think it would be elegant to just write geometry.toWKT(); Adding it to the module would make sense. While waiting for an update, adding it when the modules callback would look something like:

Microsoft.Maps.loadModule('Microsoft.Maps.WellKnownText', function(){
    Microsoft.Maps.Polygon.prototype.toWKT = function () {
        var wkt = Microsoft.Maps.WellKnownText.write(this);
        return wkt;

What if I could add this suggestions to GitHub issues for modules?

Friday afternoon geo dev thoughts.

Small example implementation;