views:

627

answers:

4

I created a hello world program to load a local kml file (borrowed from google's docs):

var ctaLayer = new google.maps.KmlLayer("http://localhost:8080/kml/cta.kml");

This does not work (nothing gets loaded).

However, when I change that line to:

  var ctaLayer = new google.maps.KmlLayer("http://gmaps-samples.googlecode.com/svn/trunk/ggeoxml/cta.kml");

it loads properly. Both kml files are identical. What do I need to do to get it to load when serving it myself? (I tried both absolute and relative paths, and I know the paths I am using are correct...)

Also I added the correct mime type to my appserver's config file:

<mime-mapping>
    <extension>kml</extension>
<mime-type>application/vnd.google-earth.kml+xml</mime-type>
</mime-mapping>
<mime-mapping>
    <extension>kmz</extension>
    <mime-type>application/vnd.google-earth.kmz</mime-type>
</mime-mapping>

But it still doesn't load.

I found this in google's docs:

The Google Maps API supports the KML and GeoRSS data formats for displaying geographic information. These data formats are displayed on a map using a KmlLayer object, whose constructor takes the URL of a publicly accessible KML or GeoRSS file.

So I guess what I am trying to do is not possible without serving the kml from a publicly accessible url...unless someone can prove otherwise

A: 

Your localhost:8080 web server is likely not providing the proper Content Type header for KML files, while googlecode.com does. I had this issue when I worked with KML.

The proper header is:

Content-Type: application/vnd.google-earth.kml+xml

Fosco
I already made sure to add this mime type but it didnt work. I just realized - does the kml need to be publicly accessible (i.e. by google) for this to work? Obviously google cannot access the kml file stores on my local machine (or does the API handle this)? Thanks
hddd
No there does not need to be any public access. When you debug your app, are you able to use that URL to get the KML file manually?
Fosco
yes, when I type in that url in the browser a download window comes up prompting me to save the kml file or open it (with google earth)
hddd
I'd double check the response headers coming from your server (using firebug, or cURL, or something of that nature)
Frank Farmer
Then can you post some/all of your KML file? I remember it took me a while to get everything right with a test placemark before I was able to make it dynamic and huge.
Fosco
my kml file is identical to the one in the link in the 2nd code sample: http://gmaps-samples.googlecode.com/svn/trunk/ggeoxml/cta.kml ..thanks for your help
hddd
I just noticed in firebug that I get a response code of 304 instead of the expected 200 when loading the kml from my server..
hddd
304 just means it has not been modified and the browser should use the cached copy.
Fosco
ah, right..this is really strange then
hddd
From google's docs: "The Google Maps API supports the KML and GeoRSS data formats for displaying geographic information. These data formats are displayed on a map using a KmlLayer object, whose constructor takes the URL of a publicly accessible KML or GeoRSS file." so i guess it does have to be publicly accessible...
hddd
Should be able to use a relative file path... might have different syntax or a different function for that. I always used a real web server.
Fosco
+4  A: 

The KML can't be accessed since it's on your local machine and google can't access that since it doesn't know where localhost:8080 .

Martin Murphy
This is correct, google needs to be able to access it. We had the same problem, because we didn't want our KML files to be accessible to the public. The remote API accesses the KML file, so it can't be local, it must be published on the web.
Kezzer
A: 

It took me a while to find this answer. Can Google please put this information in their API reference?

roblouws
+1  A: 

Unfortunately you cannot use "localhost". You have two choices:

  1. place the kml on a publically available domain. (if google cannot access it, it won't work)
  2. Use geoxml3 which basically does what google does but allows you to downlaod and host the parser JS file youself. It will allow you to load a LOCALHOST KML and parse it out for you (objects accessible via JSON) (http://code.google.com/p/geoxml3/).

Choice #1 might not be an option for those working on defense contracts and deal with sensitive information as the kml is sent to google in the background and rendered on the map.

krefftc