Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps API V3 callbacks (KML loading)

I'm using Google Maps API v3 to display a map a load some KML and display/hide them with kml.setMap() method.

I need to display a "loading" window while the KML is loading until the map is complete loaded.

I tried to use something like this:

google.maps.event.addListener(map, 'tilesloaded', function() {
    var d = new Date();
    console.log('Loaded: ' + d);
});

google.maps.event.addListener(map, 'bounds_changed', function() {
    var d = new Date();
    console.log('Started: ' + d);
});

But it didn't worked as expected.

The "titlesloaded" event is not fired always, probably because the cached images?

Here is my log:

Started: Tue Mar 29 2011 16:22:03 GMT-0300 (BRT) <-- started loading map
Loaded: Tue Mar 29 2011 16:22:06 GMT-0300 (BRT) <-- done loading map
Started: Tue Mar 29 2011 16:22:30 GMT-0300 (BRT) <-- started plotting the KML
Started: Tue Mar 29 2011 16:22:30 GMT-0300 (BRT) <-- started plotting the KML (again?!)
Loaded: Tue Mar 29 2011 16:22:32 GMT-0300 (BRT) <-- done plotting the KML

And received nothing while hiding/showing the KML again

like image 667
Thiago Belem Avatar asked Mar 29 '11 19:03

Thiago Belem


2 Answers

Try to register a listener on the kmlLayer instead of the map. I made some simple tests with listening to the metadata_changed event and it seems to work fine.

google.maps.event.addListener(kmlLayer, "metadata_changed", function() {
    console.debug("metadata_changed");
});
like image 50
Björn Avatar answered Oct 19 '22 00:10

Björn


I suppose you could write your own custom loader for KML. Here is a simple example which simply tests the return value of the KMLMetaData object. You would have to adapt this to work with multiple KML files.

<html>
    <head>
        <script type="text/javascript" src= "http://maps.google.com/maps/api/js?sensor=false">
        </script>
        <script>
    var map;
    var cta_layer;
    var loader;
    var loaderId;

    function initialize() {
        loader = document.getElementById("loader");
        var kmlUrl = 'http://code.google.com/apis/kml/documentation/KML_Samples.kml';
        var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
        var myOptions = {
            zoom: 4,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

        cta_layer = new google.maps.KmlLayer(kmlUrl, {suppressInfoWindows: true,preserveViewport:true});
        cta_layer.setMap(map);
        loaderId = setInterval("kmlLoader()", 10)
    }

    function kmlLoader() {

        if (typeof  cta_layer.getMetadata() == "object") {

            loader.style.display = "none";
            clearInterval(loaderId);
            return true;
        } else {
            return false;
        }
    }

    function show() {
        cta_layer.setMap(map)
    }

    function hide() {
        cta_layer.setMap(null)
    }
        </script>
    </head>
    <body onload="initialize()">
        <div id="loader" style="background: red; color:white;display:block;">
            Loading....
        </div>
        <div id="map_canvas"  style="height: 500px;width: 500px;">
        </div>
        <input type=button onclick="show()" value="Show">
        <input type=button onclick="hide()" value="Hide">
    </body>
</html>
like image 37
Michal Avatar answered Oct 18 '22 23:10

Michal