GeoXML3无法检索KML文件

时间:2014-02-05 17:38:43

标签: javascript google-maps-api-3 kml geoxml3

出于某种原因,我无法让geoxml3加载我的KML文件。我尝试过使用publicly available URL for the file,以及Chrome,Safari和Firefox中文件的本地引用都没有效果。当我使用Chrome时,会收到一条说明

的控制台消息
  

无法检索文件。

我的代码非常简单:

function initialize() {
var infowindow = new google.maps.InfoWindow({size: new google.maps.Size(150,50) });
var map_canvas = document.getElementById('map_canvas');
var center = new google.maps.LatLng(53, -88);
var map_options = {
  center: center,
  zoom: 4,
  mapTypeId: google.maps.MapTypeId.TERRAIN
};

var map = new google.maps.Map(map_canvas, map_options);

var myParser = new geoXML3.parser({map: map, singleInfoWindow: true});
myParser.parse('https://sites.google.com/site/.../home/kml-files/ontario_regions_yan_1.kml');
}

我已尝试过在其他问题中提供的所有各种解决方案,但无法加载文件!有什么简单的我不见了吗?

2 个答案:

答案 0 :(得分:4)

事实证明,遗憾的是我没有正确版本的geoxml3.js。我从polys branch下载了文件,一切正常。

答案 1 :(得分:3)

geoxml3使用xmlHttpRequest来检索KML,xmlHttpRequest具有相同的域限制。 KML需要与加载它的页面位于同一个域中,或者通过代理加载,该代理与加载它的页面位于同一个域中。

SO question - xmlHttpRequest and cross-site restriction

your KML on geocodezip.com through a proxy

KmlLayer doesn't have that restriction (google server is loading the KML directly)

域名必须是相同。相对URL确保。尝试在http://geocodezip.com上的页面上加载http://www.geocodezip.com中的内容无效:

geocodezip.comwww.geocodezip.com是不同的域名(无论它们看起来多么相似)。相对路径是相对于提供页面的位置(而不是geoxml3.js脚本)