Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading a local .kml file using google maps?

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

like image 702
hddd Avatar asked Aug 18 '10 17:08

hddd


People also ask

Can you import multiple KML file into Google Maps?

You can import more than one kml file into Earth for the web. Just repeat the import procedure and the files will be listed in the "KML files" section of the project panel.

What is .KML file format How do you open it?

KML is a file format used to display geographic data in an Earth browser such as Google Earth. KML uses a tag-based structure with nested elements and attributes and is based on the XML standard. All tags are case-sensitive and must appear exactly as they are listed in the KML Reference.


3 Answers

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

like image 156
Martin Murphy Avatar answered Oct 15 '22 04:10

Martin Murphy


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.

like image 40
capdragon Avatar answered Oct 15 '22 02:10

capdragon


This website, display-kml.appspot.com, requires that you copy/paste the entire KML file into the website. Alternatively, you can use Dropbox to host the KML file using your public folder. Within the public Dropbox folder, there is a right-click context menu that allows you to copy the URL.

Update:

The appspot website has a history of being unstable. As of January 2019, the website appears to be working.

REFERENCES:
  1. http://display-kml.appspot.com/
  2. https://www.dropbox.com/
like image 19
j.raymond Avatar answered Oct 15 '22 03:10

j.raymond