我正在使用Google Maps Javascript API v3将一些KML文件加载到地图上。但是,对于某些层,我会以各种方式获取TIMED_OUT,FETCH_ERROR和OK。我已经为每个人提取了网址并将其放入maps.google.com,其中图层总是加载得很好。有没有其他人有不一致的图层加载问题?更重要的是,有什么可以做的,以便谷歌地图在被问到时能够持续加载图层吗?
我已经包含了用于加载图层的代码,尽管我很确定它没有错,因为图层实际上有时会加载。
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);
}
});
}
答案 0 :(得分:1)
加载KmlLayers
取决于一些失控的事情,例如承载KML的服务器(让我假设是您的服务器)文件和必须下载它的Google服务器之间的网络。此网络(或您的服务器)越慢,Google服务器就越难以获取该文件。我很确定Google服务器不会等待你的文件太久,所以如果网络或你的服务器很慢,它可能会放弃并给你一个错误。我不认为有任何重试机制,因为我已经多次看到这种情况发生(在慢速服务器上有大文件)并且重新尝试的唯一方法是重新加载页面,以便JavaScript API再次要求该层。
有关注意事项,请参阅this answer。
答案 1 :(得分:0)
在js文档中有一个kml文件数组。
var kmlLayerArray = [];
var kmlLayerRegions = [0,0,0,0,0,0,0,0,0,0,0,0,0,]; //保持图层
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) { }
});
});
}
进料
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;