Search code examples
phpjavascriptxmlgoogle-maps-api-3kml

get coordinates from kml


I have a number of kml files with just 1 path made in each. How can I get the coordinates from the kml and turn them into an array with each set of coordinates nested inside within their own array?

example array: [[lat1, long1],[lat2, long2],[lat3,long3]]

I would prefer javascript, but I can do php as well.

example kml: http://98.214.131.200/Routes/test.kml


Solution

  • geoxml3 can be used as a stand-alone parser (although it is not particularly well tested in that mode). If you want polylines, you probably want the polys branch rather than the trunk.

    The array of coordinates is not quite in the format you requested, it looks like this:

    placemarks[].LineString[].coordinates[].{lat:Float, lng:Float, alt:Float}
    

    If each of your kml files has one "path", probably the array would be:

    placemarks[0].LineString[0].coordinates
    

    And is also available as a google.maps.Polyline object.

    Here is your example kml displayed by geoxml3, if you poke around the page with a debugger, you can see the array of coordinates.