Loading multiple .kml files in a map. Only the last one loading.

1,306 views
Skip to first unread message

Ridder Osti

unread,
Apr 13, 2011, 11:10:38 AM4/13/11
to Google Maps JavaScript API v3
Hello!

First off, the map I am doing some testing on:

http://nilsh.net/gmap2.php

I have 2 .kml files from a GPS tracker on my bike, however, only one
is showing.

The code is as following:

<script type="text/javascript">
function initialize() {
var latlng = new google.maps.LatLng(59.659, 10.717);
var myOptions = {
zoom: 12,
center: latlng,
mapTypeId: google.maps.MapTypeId.TERRAIN
};
var map = new
google.maps.Map(document.getElementById("map_canvas"),
myOptions);
var ctaLayer = new google.maps.KmlLayer('http://nilsh.net/kml/
2010/10070401.kml');
var ctaLayer = new google.maps.KmlLayer('http://nilsh.net/kml/
2010/10070701.kml');
ctaLayer.setMap(map);
}

</script>


What tremendous fail am I doing here? Do I need some parser like
GeoXML for this to work? Because I have very limited knowledge about
this.

Thanks in advance!

JFrancis

unread,
Apr 13, 2011, 2:41:46 PM4/13/11
to Google Maps JavaScript API v3
If you ignore the fact that this is a GoogleMap for a second, you'll
see where the problem lies: you declare a variable (ctaLayer), give it
a value ('http://nilsh.net/kml/2010/10070401.kml'), then declare it
again and give it a new value. ctaLayer.setMap(map); will of course
set it to the LAST declared value, which is http://nilsh.net/kml/201010070701.kml.

To fix this, declare one variable per KML layer (even as simple as
ctaLayer01 and ctaLayer02), set their values, and map them
individually:

ctaLayer01.setMap(map);
ctaLayer02.setMap(map);

That should get you what you're looking for.

-- JF

geoco...@gmail.com

unread,
Apr 13, 2011, 2:42:16 PM4/13/11
to Google Maps JavaScript API v3
On Apr 13, 8:10 am, Ridder Osti <graticul...@gmail.com> wrote:
> Hello!
>
> First off, the map I am doing some testing on:
>
> http://nilsh.net/gmap2.php
>
> I have 2 .kml files from a GPS tracker on my bike, however, only one
> is showing.
>
> The code is as following:
>
> <script type="text/javascript">
>   function initialize() {
>     var latlng = new google.maps.LatLng(59.659, 10.717);
>     var myOptions = {
>       zoom: 12,
>       center: latlng,
>       mapTypeId: google.maps.MapTypeId.TERRAIN
>     };
>     var map = new
> google.maps.Map(document.getElementById("map_canvas"),
>         myOptions);
>                 var ctaLayer = new google.maps.KmlLayer('http://nilsh.net/kml/
> 2010/10070401.kml');
>                 var ctaLayer = new google.maps.KmlLayer('http://nilsh.net/kml/
> 2010/10070701.kml');

there is only one ctaLayer, so only one kml is displaying, creating
layer1/layer2 should work.

--Larry
Reply all
Reply to author
Forward
0 new messages