Search code examples
angulartypescriptfilekml

Angular2 generate KML file from Array of coordinates


I'm working in an Angular 2 v4 webapp and I need to generate a KML file with the data of an array that I get from my REST. The response of my REST is something like this:

[
    { "lat": 14.05891566, "lng": -19.9981566 },
    { "lat": 14.05668566, "lng": -19.9566123 },
    { "lat": 14.05567413, "lng": -19.9467456 },
    { "lat": 14.05455655, "lng": -19.9367125 }
    ...
]

the REST just response that array of coordinates

How can I generate a KML file with that data and then download the file .kml?

If possible, I am looking for a node module that allows me to do this, but if it does not exist, what would be the correct way to do it?


Solution

  • Assuming your coordinates represents points, I made a quick working example on StackBlitz.

    The process is simple, you have to create a XML document and create the appropriate nodes to match the KML file format in your javascript. You can then let your users download the file using data URIS.

    Here are some stackoverflow usefull topics that I used for my example: create xml with javascript, how-to-create-a-file-in-memory-for-user-to-download-but-not-through-server.

    Hope it helps!