Industry guru Dave Taylor offers free tech support on a wide variety of technical and business topics, including HTML, Apple iPhone, online advertising, Cascading Style Sheets, Web design, management, Unix, Linux, search engine optimization, online dating, Mac OS X, shell script programming and Microsoft Windows.

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?


Dave's Answer:

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    

Subscribe!

Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.

Comments

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.

http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=-81.561995,+28.357403&sll=37.0625,-95.677068&sspn=33.984987,65.917969&ie=UTF8&ll=-79.935918,28.476563&spn=40.702327,263.671875&z=2&iwloc=A

Posted by: Ken B at April 20, 2009 9:01 AM

Thanks 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 PM

I have a lot to say, but ...
Starbucks coffee cup I have a lot to say, and questions of my own for that matter, but most of all I'd like to say thank you for all your efforts on this Web site by buying you a chai!

I do have a comment, now that you mention it!











Remember personal info?


Please note that I will never send you any unsolicited commercial email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.









Uniblue: Free Virus Scan

Follow me on Twitter @DaveTaylor

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!

Add to Google Reader
Add to My Yahoo!
Subscribe in NewsGator Online

RDF   XML

Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.


Recent Entries
Book Links
© 2002 - 2009 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.