Recently we've been using some of our own tools in other projects and found some room for improvement on the KML Loader we developed. In particular we wanted a simple class that could handle all the processing from the loader, adding the resulting overlays to the google map. Also we needed it to load in some of the styles, such as colours and line types.
So we've expanded our
KmlLoader class and provided a few additional utility classes.
Before we get into it, to get the latest version, just checkout from our public repo:
The most useful is the
SimpleKmlLoader class. This simplifies the loading and display of a kml file to a single line of code.
- new SimpleKmlLoader( _map, true ).load( _file );
Generally you'll probably want to do something more with the kml than just display it, but this is handy for those quick display situations. You'll probably more want to do something like this, adding an event listener for when the file is loaded:
- var _loader:SimpleKmlLoader = new SimpleKmlLoader( _map, true );
- _loader.addEventListener( Event.COMPLETE, loader_completeHandler );
- _loader.load( _file );
The second parameter provided to the constructor is an option to automatically add the objects to the map when the load is complete, so you no longer have to worry about adding the features to the map.
Once the loader has completed you'll have access to all the objects loaded from within the file. Each of the Kml Objects is represented by the
KmlDisplayObject class, which has automatically created the appropriate type of map feature to add when required.
The important properties are
SimpleKmlLoader.objects which is an array of
KmlDisplayObject and then
KmlDisplayObject.children contains all the children of that object (again an array of
- import com.distriqt.gmaps.kml.utils.*;
- * Iterates over the objects and traces out the name of
- * each and recurses into the children of each object
- * @param _objects An array of KmlDisplayObjects
- * @param _prefix A string to prefix to the output trace
- function traceObjects(
- _objects:Array /* of KmlDisplayObject */,
- _prefix:String = "" ):void
- for each (var _object:KmlDisplayObject in _objects)
- trace( _prefix+" "+_object.name );
- traceObjects( _object.children, _prefix + "\t" );
- function loader_completeHandler( _event:Event ):void
- traceObjects( _loader.objects );
The trace output from the function above would look something like the below:
districts for BSI kangaroo ground kinglake west kinglake ...
There is an example usage of the class in the svn,
gmaps/examples/kmlloader/. It will load the kml file shown in the image above.
We've also added some Kml editing and saving tools into the svn which we'll leave those for the subject of another post but feel free to check out the code.