3

I'm using Google Maps Javascript API v3 to load some KML files onto a map. However, I variously get TIMED_OUT, FETCH_ERROR, and OK returned for certain layers. I've taken the URL for each and put it into maps.google.com where the layers invariably load just fine. Has anyone else had issues with inconsistent layer loading? More to the point, is there anything that can be done so that Google Maps will consistently load the layers when asked?

I've included the code that I use for loading the layer although I'm fairly certain that it is not at fault because the layers do actually load sometimes.

    function addMapLayer(url) {
        console.log('Attempting to add layer at: ' + url);
        var layer = new google.maps.KmlLayer(url, {
            preserveViewport: true,
            map: maps.BirdMap.map
        });

        google.maps.event.addListener(layer, 'status_changed', function () {
            console.log('KML load: ' + layer.getStatus());
            if (layer.getStatus() != 'OK') {
                $('#maps-error').text('[' + layer.getStatus() + '] Google Maps could not load the layer. Please try again later.');
                $('#maps-error').dialog('open');
            } else {
                layers.push(layer);
            }
        });
    }
user1202947
  • 31
  • 1
  • 2

2 Answers2

1

Loading a KmlLayers depends on several things that are out of control, e.g. the network between the server that hosts the KML (let me assume that's your server) file and the Google server that has to download it. The slower this network (or your server), the easier that Google server cannot get the file. I'm pretty sure Google server won't wait too long for your file, so if the network or your server are slow, it'll probably give up and give you one of those errors. And I don't think there is any re-try mechanism, since I've seen this happen several times (with big files on slow servers) and the only way to re-try is re-loading your page, so that the JavaScript API asks for the layer again.

See this answer for some things to watch out for.

Community
  • 1
  • 1
miguev
  • 4,481
  • 21
  • 41
  • Thanks, that's what I was afraid of. I guess I'll look into making the layers smaller and less complex since those tend to load more reliably than others. – user1202947 Feb 13 '12 at 15:37
0

i have had similar problems.

  1. you need to use a schedular
  2. you need to use memcache
  3. schedules job will load all the kml files and store them in cache
  4. use your js to run jquery load kml file to display
  5. have an array for kml files in js document.

    var kmlLayerArray = [];
    

    var kmlLayerRegions = [0,0,0,0,0,0,0,0,0,0,0,0,0,]; // Hold layers

  6. kmlLayer = new google.maps.KmlLayer(url);

            kmlLayer.setValues({
                suppressInfoWindows : true
            });
    
            kmlLayer.setValues({
                preserveViewport : bool
            });
            kmlLayer.setMap(map);
    
            kmlLayerArray.push(kmlLayer);
            kmlLayerRegions[region] = kmlLayer;
    
            addKmlEventListener(map,kmlLayer); //extra stuff
    

function addKmlEventListener(map,kmlLayer){ google.maps.event.addListener(kmlLayer,'click',function(kmlEvent) {

    $.ajax({
        url : 'http://' + top.location.host + '/mcmap/feed/kml.php?action=kmlclick',
        data : "id=" + surveyid,
        dataType : 'json',
        success : function(data) {

            // do whatever
        },
        error : function(jqXHR, textStatus,
                errorThrown) { }
    });

});

}

feed

if (!$contents = $cache->load("kml_survey_id_" . $surveyId)) {
            $log->debug("not cached");

            $kml = $mapper->getSurveyKML($seismicLibrary->getSurvey($surveyId), "#FFFFFF"); // format kml file here
            $zip = new ZipArchive();
            $filename = "/tmp/" . time() . rand() . ".zip";
            $res = $zip->open($filename, ZipArchive::CREATE);
            if ($res === TRUE) {

                $zip->addFromString("doc.kml", $kml);
                $zip->close();
            }

            $file = fopen($filename, "rb");
            $contents = fread($file, filesize($filename));
            fclose($file);

            unlink($filename);
            //$log->debug($kml);
            $cache->save($contents, "kml_survey_id_" . $surveyId);
        }

        header('Content-type: application/vnd.google-earth.kml+xml');
        //header('Content-type: application/vnd.google-earth.kmz');

        echo $contents;
shorif2000
  • 2,582
  • 12
  • 65
  • 137