Display KML Files in Google Maps with GGeoXML
I just wanted to share a cool way that you can display your existing KML files inside a Google Maps application using the GGeoXML class from the Maps API. Many organizations have pre-existing KML files that were created for use within Google Earth, and the GGeoXml Maps API class provides a really simple means for also displaying this data in Google Maps as a GOverlay. Although we are specifically using this class to read a KML file in this example it is worth noting that this class can be used to read any XML file.
In this example, we’re displaying an auto-updating KML file containing Global MODIS Hotspots provided by the Fire Information for Resource Management System (FIRMS) at the University of Maryland. FIRMS integrates remote sensing and GIS technologies to deliver global MODIS hotspot/fire locations to natural resource manager and other stakeholders around the world. FIRMS is funded by NASA and builds on Web Fire Mapper, a web mapping interface that displays hotspots/fires detected by the MODIS Rapid Response System and delivers near real-time hotspot/fire information to international users.
FIRMS provides several auto-updating KML files for regions around the world. Any of these files can be loaded into Google Earth. What I’ve done is take the Continental USA KML file which is updated every four hours (as are the other files) and dynamically loaded the file into a simple Google Maps application using the GGeoXML class in the Maps API.
Take a look at this example and then I’ll describe how this is done within Google Maps.
From a code standpoint, the Maps API makes this a really easy task which can be done with only a few lines of code. To see the full code for this simple Maps application you can simply right click the web page and click View –> Source. I’ve posted the relevant code below. The initialize function is called when the web page loads. We create a new instance of the GGeoXML class and pass in a parameter to the constructor which contains a pointer to a KML file located on a remote web server. In this case we’re pointing to the Continental USA KML file located on the FIRMS web server. Once we’ve created an instance of GGeoXML we then add it to the map using the addOverlay( ) method on the GMap2 class. In between we have some additional Maps API code which creates the underlying map, centers the map, and adds the map controls.
As you can see, using the GGeoXML class to add pre-existing KML files to your Google Maps application is quite simple and very useful. Even the descriptive balloons contained within the KML file have been ported to info windows in Google Earth. Click one of the fire locations to return specific information about a particular fire.
For more information on the Google Maps API or GeoSpatial Training Services please see our e-learning course entitled “Google Maps For Your Apps” or our “Google Maps Developer Bundle” which is currently on sale as part of our 3rd Year Anniversary Sale.