KML in Google Maps and Earth
Tuesday, 31 August 2010
Article Index
KML in Google Maps and Earth
Maps and the MAP API
Earth and its extensions


Maps or Earth?

There are a number of different ways of displaying a KML file and they are each worth knowing about. You can display the file using either Maps or Earth. Each has some advantages and disadvantages so you do need to know how to work with both.

Google Maps doesn't need a developer key to use and it doesn't need the user to download a browser plugin or desktop application. It is also more suitable for use on mobiles.

However Google Maps has one disadvantage that is important to know about and understand the implications of. Using Google Maps you can only load a KML file that is accessible via a public URL. You also cannot get access to the objects created by the KML file via script. What this means is that you can create and use a KML file that is located on a server and you can even manipulate the file server side but you can't load or manipulate a KML file on the client side.

Google Earth on the other hand has facilities to load KML file from a URL or from a local file and you can work with the object created by the KML file. That is you can do both server and client side manipulation of KML.

KML in Google Maps

Google Maps can only load a KML file from a publicly accessible URL. There are also a number of ways of doing this via the Google Maps web site and within your own code.

From within Google Maps you can either use a URL that corresponds to the location of the KML file you have created or you can upload a local file and Google will host it for you internally. To do this you need to have a Google account.

The simplest way of getting KML onto a map is if it is hosted by a web server.For example the demo file constructed earlier is available from the URL

If you type, or copy and paste this into the search box on Google Maps you will be taken to view the default pushpin marker.


You can also save your KML to MyMaps and reload it when ever you need to - provided the file is still at the same URL. You also need to know that Google Maps caches the KML file and so any changes you make to it aren't necessarily visible when you refresh the page.

If you do have a Google account you can upload a local KML file and have Google host it for you. To do this first log in to your account on the Google Maps page or into any Google application. Next display a Google Map in your browser and click on My Maps and then click on Create New map - the menus concerned are on the left of the map:

mymapsGive the map a title and description if you want to and select either Public or Unlisted for a private map.

Next click Import and either supply a URL or click the browse button and navigate to the KML file on your local machine. When you have selected the file, click the upload button and the file is uploaded to the Google map server.

The maximum file size supported is 10MB. Notice that this facility works by uploading and hosting your KML file on Google's own server so it isn't really directly accessing a local file.

If the KML file is big then you can significantly reduce its size by Zipping it. All you have to do is Zip the file and rename it to have a .KMZ extension. A KMZ file can be used just like a KML file and the application using it will automatically unzip the file after downloading it.

Sharing Maps

If you want to share your KML files then uploading them to Google Maps is an easy solution -. simply set them to Public. You can even set up a collaborative project.

You can also get Google to take notice of your KML/KMZ files that you host and offer them to a wider public audience in response to searches.

To do this you have to

  1. Create your KML/KMZ file
  2. Make sure it contains attribution tags that contain information such as author, name, url to your website etc.
  3. Post the KML/KMZ to a publicly accessible webserver
  4. Create a Geo site map - that is an XML format file that describes where on the webserver there are KML/KMZ files that you would like Google to index.
  5. Submit the Sitemap to Google search in the usual way.

KML via the Map API

Of course if you are hosting Google Maps in your own web page, as described in Getting started with Google Maps then the obvious thing to do is to find our how to display KML within a hosted map. This turns out to be very easy.

First let's tackle the case where the KML file is available via a publicly accessible URL. In this case all we have to do is create a KmlLayer object using the URL in the constructor. This downloads and interprets the KML file and when you add the KmlLayer to the map the graphics it specifies appear. The technique works with KMZ files as well.

For example, assuming that we already have the code to create a map object then the demo1.KML file can be loaded using:

var kml=new google.maps.KmlLayer(

Notice that there is no way of loading KML generated by Javascript say directly into a map. In Google maps all KML has to be hosted at a public URL. There are obvious ways round this but they are too difficult to go into here and will be covered in a future project.







Last Updated ( Tuesday, 31 August 2010 )

RSS feed of all content
I Programmer - full contents
Copyright © 2014 All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.