Adrian Ber Adrian Ber - 5 months ago 34
JSON Question

Google My Maps - data layer as JSON

Is there any way that I can access a data layer from Google My Maps as JSON? Basically I want the same data that you get by selecting "Open data table" plus, of course, the coordinates.

Answer

I used togeojson. And here's the code.

var fs = require("fs");
var request = require("request");
var tj = require('togeojson');
var jsdom = require('jsdom').jsdom;

var requestOptions = {
        headers: {'user-agent': 'node.js'}
        , followRedirect: true
        , followAllRedirects: true
};


function retrieveTrack(track, trackUrl) {
    console.log("Retrieving track " + track + " from " + trackUrl);
    request(trackUrl, requestOptions, function (error, response, body) {
        console.log("Response:" + response.statusCode);
        if (!error && response.statusCode == 200) {
            fs.writeFile(track + ".kml", body, function(err) {
                if(err) {
                    return console.log(err);
                }
                console.log("Track retrieved: " + track);
            });
            convertTrack(track);
        }
    });
}

function convertTrack(track) {
    fs.readFile(getKmlPath(track), function (error, data) {
        var json = tj.kml(jsdom(data));
        fs.writeFile(track + ".json", JSON.stringify(json, null, "\t"), function(err) {
            if(err) {
                return console.log(err);
            }
            console.log("Track converted: " + track);
        }); 
    });
}
Comments