开发者

How to display KML file using javascript

开发者 https://www.devze.com 2023-01-17 17:02 出处:网络
How to dis开发者_JAVA技巧play google earth (KML) file using javascript. thanksI\'ll assume you want to view the KML-file in a browser preferably Mozilla Firefox. Firstly, I want to give some simple J

How to dis开发者_JAVA技巧play google earth (KML) file using javascript.

thanks


I'll assume you want to view the KML-file in a browser preferably Mozilla Firefox. Firstly, I want to give some simple JavaScript that we will use in OpenLayers. You should try them since it also using JavaScript library.

Here I briefly show you how the codes are written.

<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>

As you can see, there is a small orange dot on the map. That is the KML file loaded on Google Maps.

Try and run the HTML codes using any software. But, I suggest you to use Notepad ++. Last but not least, I hope my answer isn't too late for you, Kyathi.


//do axis call to the data url 
// parse the records to get the point and additional data 
//plot like this 
var features_1 = new Array(records.length -1);
    for (var i = 1; i < records.length; i++) {
      var coordinates = transform([  records[i][9], records[i][8]], 'EPSG:4326', 'EPSG:3857');
      features_1[i-1] = new Feature( {
        geometry: new Point(coordinates ),
        name: records[i][6],
        Magnitude: dat[i][dat[0].length -1],
        size : dat[i][dat[0].length -1]
      }) ;

  }
  var source = new VectorSource({
  features: features_1
  });

  var clusterSource = new Cluster({
    distance: 30,
    source: source
  });


vector = new VectorLayer({
  source: clusterSource,
  style: styleFunction
});

var raster = new TileLayer({
  source: new Stamen({
    layer: 'toner'
  })
});

var map = new Map({
  layers: [raster, vector],
  interactions: defaultInteractions().extend([new Select({
    condition: function(evt) {
      return evt.type == 'pointermove' ||
          evt.type == 'singleclick';
    },
    style: selectStyleFunction
  })]),
  target: 'map',
  view: new View({
    center: [0, 0],
    zoom: 2
  })
});

//

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号