GeoXML3 未定义,无法在自己的服务器上查看 KML 文件中的数据?

GeoXML3 is not defined and cannot view data from KML file on own server?

我尝试按照以下示例进行操作,https://gist.github.com/webapprentice/8427539, which when ever I try to display their KML from an external link I am able to display the polygon correctly, however if I copy and past the KML into a file and place this file on my server I am not able to display the results for the KML file. This is true of any third party KML file. Interestingly, I've noted that the coordinate values are inverse, but whenever I switch these values using http://kmltools.appspot.com/geoconv the KML file still does not function properly. I assume that the KML information is valid, because when I enter the contents into the text box at http://display-kml.appspot.com/ 左边的地图正确显示了我的多边形。

为了找到解决方案,我已将 geoxmlv3.js 添加到我的服务器并尝试将 KML 文件解析为我的地图可以理解的格式,因为我假设我的服务器无法呈现KML 文件正确。您可以在 HTML & JavaScript 下方查看 KML 文件的结果。 GeoXML 给了我两个错误:Missing catch or finally after trygeoXML3 is not defined。这些错误对我来说没有多大意义,因为我已经在我的代码中清除了上面的文件,您可以在下面看到。

此外,我在 IIS 中添加了一个 mime 类型,其中文件扩展名为 kml,类型为 application/vnd.google-earth.kml+xml。我相信这种 mime 类型是有效的,因为当页面被定向到文件时,文件会像任何其他第三方 KML 文件一样被下载。

========= HTML & JavaScript ==========

<!DOCTYPE html>
<html>
 <head>
<title>Simple Map</title>
<meta name="viewport" content="initial-scale=1.0">
<meta charset="utf-8">
<style>
  html, body {
    height: 90%;
    margin: 0;
    padding: 0;
  }
  #map {
    height: 100%;
  }
</style>

 <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&key=AIzaSyAjC_rKyBOaIBHtTu39GjC8h4SFWxry-s4"></script>
 <script src="/js/geoxmlv3.js"></script>
<script>
  var map;
  var mylocation = {
     'latitude':  47.66,
'longitude': -122.355
  }

   function initialize() {
var myLatlng = new google.maps.LatLng( mylocation.latitude,     mylocation.longitude );
var mapOptions = {
  zoom: 12,
  center: myLatlng,
  mapTypeId: google.maps.MapTypeId.ROADMAP
}
    var map = new google.maps.Map(document.getElementById('map'), mapOptions);

    var myParser = new geoXML3.parser({map: map}); 
    myParser.parse('/zoneManagers.kml'); 


// This needs to be the Full URL - not a relative URL
// var kmlPath = "zoneManagers.kml";
// // Add unique number to this url - as with images - to avoid caching issues during development
// var urlSuffix = (new Date).getTime().toString();
// var layer = new google.maps.KmlLayer(kmlPath + '?' + urlSuffix );
//layer.setMap(map);
  }
  google.maps.event.addDomListener(window, 'load', initialize);



</script>
  </head>
  <body>
 <div id="map"></div>
  </body>
  </html>

===== KML 文件 =====

https://www.dropbox.com/s/q4v8eru0o3zp8xq/zoneManagers.kml?dl=0

关于在您的服务器上托管时无法显示 KML 文件的结果。你说的是什么服务器?您是在谈论开发服务器 运行 例如在本地主机上,本地 LAN 中的服务器还是任何人都可以访问的 public 服务器? 您是否像 gist http://web-apprentice-demo.craic.com/assets/maps/fremont.kml 中那样为 KML 使用完全限定的 URL ?您指定的 URL 是否可以在任何浏览器中设为 copy/pasted 以不受任何限制地访问该文件?

Google 需要能够访问 KML 文件才能对其进行处理,并且它正在使用您指定的 URL 来访问它。如果您 运行 在本地主机上,私人服务器或未指定 public 完全合格的 URL,Google 无法访问它。

如果您不这样做,则需要将 KML 文件放在 public 可用域上并指定完全限定的 public URL.

如果您处理的是敏感信息,则此选项并不总是可用,因此您使用 geoxml3 的想法是最佳选择。

我没问题运行您提供的代码并成功显示地图和 KML 文件。

geoXML3 is not defined 以来,我认为您可能没有正确导入文件。

  • 你的 geoxmlv3.jsjs 文件夹中吗?
  • 您导入的文件是否正确? (source)
  • 您是否将名为 geoxml3.js 的官方文件重命名为 geoxmlv3.js 因为您尝试导入 /js/geoxmlv3.js 而不是 /js/geoxml3.js
  • js 文件夹是否在您服务器的根目录?您可能需要使用 js/geoxml3.js 而不是第一个 /.

您还可以在浏览器检查器(网络选项卡)中检查 geoxmlv3.js 是否已正确加载。

200 状态码表示 geoxmlv3.js 已正确加载。