
How do I create a KML map data file?Okay, so I have been reading some of your other blog entries here and you've written both about how to convert street addresses into map data coordinates (see convert street map addresses into latitude and longitude) and how to import Keystone map data (KML) files into Google Maps (see Import KML data files into Google Maps). My question lies in the middle: how do I take lat/long data and create a KML data file? You've got me on that one. In fact, I ended up writing a shell script that accepts a set of street addresses and outputs a KML data file suitable for importing in Google Maps or any other mapping system that supports the KML data file format. It's too complex to share here, however, (it's actually for a book I'm working on, but that's another topic of discussion!) but I can show you the rudiments of KML data files so you can easily copy and paste to produce what you seek. At its most basic, KML is just a specific type of XML data file, which means that it looks a lot like HTML, the language of Web pages. You can learn more about XML in a lot of different places, but here's a link to the Wikipedia entry on XML as a good place to start. Let's just jump in. Here's what a very succinct one-data-point KML file would look like: <?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2"> <Placemark> <name>name of place</name> <description>longer description</description> <Point> <coordinates>-81.561995,28.357403</coordinates> </Point> </Placemark> </kml> Pretty confusing, isn't it? Let me show it to you again, but this time I will change the information that is related to the specific data point (an address on the Walt Disneyworld property in Orlando, Florida, btw!): <?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2"> <Placemark> <name>name of place</name> <description>longer description</description> <Point> <coordinates>-81.561995,28.357403</coordinates> </Point> </Placemark> </kml> Not so bad once you look at that. Now let's change it once more, but this time add helpful prose for the name and description: <?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2"> <Placemark> <name>Disney's Hollywood Studios</name> <description>Disney's Hollywood Studios is a theme park at the Walt Disney World Resort. Spanning 135 acres in size, its theme is show business, drawing inspiration from the heyday of Hollywood in the 1930s and 1940s. The third park built at the resort, it opened on May 1, 1989 as Disney-MGM Studios. In 2007, the park hosted approximately 9.51 million guests.</description> <Point> <coordinates>-81.561995,28.357403</coordinates> </Point> </Placemark> </kml> NOTE: Here's a weirdness you should know if you want to try and put all the pieces of the puzzle I've presented together, though: while the KML files are in long/lat order, the data you enter into Google Maps on a search to get a point plotted are in lat/long order. In other words, if you were to just copy and paste the point coordinates shown above, you'd end up not on this Earth. You can't just map -81.561995, 28.357403, in other words, you need to reverse the order of the two if you want to just search for that point in Google Maps: 28.357403, -81.561995. I have no idea why they aren't consistent! If you'd like, copy the KML snippet above, save it as "sample.kml" and feed it to Google Maps to see what happens (as I explain here: Import KML data files into Google Maps). But what you probably want is to have more than one data point in the KML file. This can be done by duplicating the "Placemark" block of KML. To illustrate this, I'll go back to the uncommented KML file (so it's a bit shorter): <?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2"> <Placemark> <name>name of place #1</name> <description>longer description #1</description> <Point> <coordinates>coords #1</coordinates> </Point> </Placemark> <Placemark> <name>name of place #2</name> <description>longer description #2</description> <Point> <coordinates>coords #2</coordinates> </Point> </Placemark> </kml> See what I've done here? Turns out you can easily duplicate this material again and again, though it's a wee bit tedious. Finally, here's a simple three-point KML data file you can download and examine for yourself that will illustrate the points I've made here. I hope it's interesting: wdw.kml (right-click or cmd-click on it and choose "save link to file").
Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Stumble Upon
Categorized:
CGI Scripts and Web Site Programming
(Article 8853)
Tagged: disneyworld, geocaching, google maps, kml, mapping, wdw Previous: How can I convert map addresses into latitude longitude? Next: Advertising through Social Networks? Subscribe!
Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader. Of course you can map "-81.561995, 28.357403". It's in Antarctica, south of Africa, and therefore a bit colder than Orlando, but it exists. Now, had you tried something further west in the original coordinates, such that it was further than 90 degrees longitude, that would be a different matter, as latitude can't go beyond 90. Posted by: Ken B at April 20, 2009 9:01 AMThanks for a very good tip Dave. This is especially helpful for me. Posted by: Adventurous Wench women's travel at May 2, 2009 10:04 PMI have something to say, now that you mention it, but ...
I do have a comment, now that you mention it!
|
![]() ![]()
Search
Find just the answers you seek from among our 2300+ free tech support articles by using our Lijit search engine.
Help!
Subscribe to
Ask Dave Taylor!
Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.
Articles and Reviews
Auctions and Online Shopping Blogs and RSS Feeds Building Web site traffic Business and Management Cell Phones and Mobile Phones CGI Scripts and Web Site Programming Computer and Internet Basics d) None of the Above HTML and CSS Industry News and Trade Shows Mac OS X Help MySpace, Facebook, Twitter and Social Network Help Pay Per Click (PPC) Search Engine Optimization Shell Script Programming Sony PSP, MP3 Players, Etc. The Writing Business Unix and Linux Help Video Game Tips and Help Windows Help
Recent Entries
Book Links
|