3

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?

Sergio Mendez
  • 1,311
  • 8
  • 33
  • 56

1 Answers1

4

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!

GeoAstronaute
  • 1,751
  • 1
  • 11
  • 12
  • thank you very much. This is exactly what I was looking for – Sergio Mendez Jan 13 '20 at 06:05
  • It's a very nice example! But in my case markers imported from the resulting KML file showed wrong locations on Google Earth. The reason was that uses longitude, latitude order. If you swap the order of coordinates in the example, everything works perfectly. – Arthur Z. Sep 07 '22 at 06:56