如何使用javascript显示KML文件

Aad*_*adi 2 google-maps

如何使用javascript显示谷歌地球(KML)文件.

谢谢

小智 5

我假设你想在浏览器中查看KML文件,最好是Mozilla Firefox.首先,我想提供一些我们将在OpenLayers中使用的简单JavaScript.您应该尝试它们,因为它也使用JavaScript库.

在这里,我简要介绍如何编写代码.

<html>
<head>
<title>Google Layer with KML file</title>
<link rel="stylesheet" href="http://openlayers.org/api/theme/default/style.css" type="text/css" />
<link rel="stylesheet" href="http://openlayers.org/dev/examples/style.css" type="text/css" />
<script src='http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAl9RMqSzhPUXAfeBCXOussRTQDbvAygy0cfGJr8dEMAYKf3RWNBQqP9mjKIsqTfmAlz5LOJ3Xpy5s4w'></script>
<script src="http://openlayers.org/api/OpenLayers.js"></script>
<script type="text/javascript">

var map;

function init() {
    // Create the map object
    map = new OpenLayers.Map('map');
    // Create a Google layer
    var gmap = new OpenLayers.Layer.Google(
        "Google Streets", // the default
        {numZoomLevels: 20}
    );
    // Add layer to map
    map.addLayer(gmap);

    //Adding KML file to map
    map.addLayer(new OpenLayers.Layer.GML("KML", "kompleks_antarabangsa.kml", 
           {
            format: OpenLayers.Format.KML, 
            formatOptions: {
              extractStyles: true, 
              extractAttributes: true,
              maxDepth: 2
            }
           }));
    // Zoom to Kuala Lumpur, Malaysia
    map.setCenter(new OpenLayers.LonLat(101.686855,3.139003), 13);         
}
</script>
</head>
<body onload="init()">
<h1 id="title">Google Layer with KML file</h1>
<div id="map" style='width: 700px; height: 700px'></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

如您所见,地图上有一个小橙点.这是Google地图上加载的KML文件.

尝试使用任何软件运行HTML代码.但是,我建议你使用Notepad ++.最后但并非最不重要的是,我希望我的答案对你来说为时已晚,Kyathi.